If statement in user defined function within apply in R -
forgive me if blatantly obvious question, beginner r user eager learn.
i have data frame of 4 columns 1.5 million rows containing coordinate information each individual row represents specific location. run these data function holds series of if else statements determine area of specific location within larger box. example, point can in center, along edge of box within 1.5 inches, on inside of box not on edge nor @ center, or on outside of box.
each if statement determines if set of points in specified area, and, if is, result if statement putting '1' in corresponding row of data frame.
here visualization of trying do:
take location data data frame called 'dimensions':
sz_top | sz_bot | px | pz | 3.526 | 1.615| -1.165| 3.748 |
run through these statements (the real statements longer), 'else' condition means point outside box completely:
if(in center) else if(on edge) else if(in box, not in center or on edge) else
when program finds condition true, puts 1 in data frame called 'call' in corresponding column (these columns columns 50-53). row in event code found point in center:
center| edge| other_in| out| 1 | 0 | 0 | 0|
one thing note improve efficiency coordinates contained in 'calls' data frame in columns 22,23,26, , 27, moved them 'dimensions' because easier me work with. can changed.
i unclear on how proceed here. have if else statement written, unclear on how program know row on correctly mark corresponding row result of tests.
please let me know if more information me.
thanks!
edit:
here sample of 'dimensions' data frame:
sz_top sz_bot px pz 1 3.526 1.615 -1.165 3.748 2 3.29 1.647 -0.412 1.9 3 3.29 1.647 -1.213 1.352 4 3.565 1.75 -1.041 2.419 5 3.565 1.75 -0.357 1.776 6 3.565 1.75 0.838 0.834 7 3.541 1.724 -1.619 3.661 8 3.541 1.724 -2.498 2.421 9 3.541 1.724 -1.673 2.348 10 3.541 1.724 -1.572 2.982 11 3.305 1.5 -1.316 2.842
here example of 1 of if statements. others similar, looking @ different locations around box in question:
if( ((as.numeric(as.character(dimensions$px))*12)>= -3) && ((as.numeric(as.character(dimensions$px))*12)<= 3) && ((as.numeric(as.character(dimensions$pz))*12)<=((as.numeric(as.character(dimensions$sz_top))*12-as.numeric(as.character(dimensions$sz_bot))*12)/2)+(as.numeric(as.character(dimensions$sz_bot))*12)+3) && ((as.numeric(as.character(dimensions$pz))*12)>=((as.numeric(as.character(dimensions$sz_top))*12-as.numeric(as.character(dimensions$sz_bot))*12)/2)+(as.numeric(as.character(dimensions$sz_bot))*12)-3) ){return(1) }
if understand correctly, following return numeric vector of ones , zeros can slot appropriate column of calls
.
dimensions <- read.table(text='sz_top sz_bot px pz 1 3.526 1.615 -1.165 3.748 2 3.29 1.647 -0.412 1.9 3 3.29 1.647 -1.213 1.352 4 3.565 1.75 -1.041 2.419 5 3.565 1.75 -0.357 1.776 6 3.565 1.75 0.838 0.834 7 3.541 1.724 -1.619 3.661 8 3.541 1.724 -2.498 2.421 9 3.541 1.724 -1.673 2.348 10 3.541 1.724 -1.572 2.982 11 3.305 1.5 -1.316 2.842', header=t, row.names=1) as.numeric( dimensions$px*12 >= -3 & dimensions$px*12 <= 3 & dimensions$pz*12 <= (dimensions$sz_top*12 - dimensions$sz_bot*12)/2 + (dimensions$sz_bot*12) + 3 & dimensions$pz*12 >= (dimensions$sz_top*12 - dimensions$sz_bot*12)/2 + (dimensions$sz_bot*12) - 3)
by using single ampersands, r evaluates conditional expression each row of data.frame, rather stopping when condition first not met.
i've removed as.numeric
, as.character
clarity (not sure why these necessary anyway... these data read in factors? if so, perhaps try stringsasfactors = false
).
Comments
Post a Comment