Nebulo Archae
2014-Sep-29 23:05 UTC
[R] If statement not working in a for loop when making it independent
Dear all, I have a data.frame xy that contains numeric data and data_qual which contains qualitative data which I want to include in a for loop with an if statement (commented out in the code below). The if statement should be applied if the ID in data_qual$ID is the same than in xy$ID. I am trying to make it independent by doing this: if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){...} ? but it doesn't work when I incorporate it in the for loop. The logical result is correct but it only works for the first element when implemented in the for loop. Can anyone see what I am doing wrong here? ####################### Here is my code: xy <-data.frame(NAME=c("NAME1","NAME1","NAME1","NAME2","NAME2","NAME2","NAME3","NAME3"),ID=c(87,87,87,199,199,199,233,233),X_START_YEAR=c(1950,1988,1994,1899,1909,1924,1945,1948),Y_START_VALUE=c(75,25,-90,-8,-55,-10,-9,12),X_END_YEAR=c(1985,1994,1999,1904,1924,1987,1946,1949), Y_END_VALUE=c(20,50,-15,-70,-80,-100,24,59)) ? data_qual <- data.frame(NAME=c("NAME2","NAME3"),ID=c(199,233),X_START_YEAR=c(1986,1905), Y_START_VALUE=c("-X","ST"),X_END_YEAR=c(1987,1907),Y_END_VALUE=c("-X","ST")) ? # split xy by group as defined by ID ind <- split(xy,xy$ID) ? for (i in ind){xx = unlist(i[,grep('X_',colnames(i))])? ?? ? ? ? ? ? ? yy = unlist(i[,grep('Y_',colnames(i))])? ?? ? ? ? ? ? ? fname <- paste0(i[1, 'ID'], '.png')? ?? ? ? ? ? ? ? png(fname, width=1679, height=1165, res=150) ? if(any(xx < 1946)) {my_x_lim <- c(min(xx), 2014)} else {my_x_lim <- c(1946, 2014)}? ? par(mar=c(6,8,6,5)) ? plot(xx, yy,main=unique(i[,1]),xlab="Time [Years]",ylab="Value [m]",pch=21,xlim = my_x_lim,font.lab=2, cex.lab=1.2, cex.axis=1.1) ? i <- i[,-1] ? segments(i[,2],i[,3],i[,4],i[,5],lwd=2) ? ?# if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){ ?# ? ? rel_data_qual <- data_qual[data_qual$ID %in% i$ID,] ?# ? ? text(x = rel_data_qual$X_END_YEAR, ?#? ? ? ? ? y = min(i$Y_END_VALUE + 3), ?# ? ? labels = rel_data_qual$Y_END_VALUE) ? ?# ? } ? ? dev.off() } Thanks, Kurt
Jeff Newmiller
2014-Sep-30 00:33 UTC
[R] If statement not working in a for loop when making it independent
You have really tied yourself up in a knot here. Last I checked, when A==B, then B==A. You also need to study the difference between ?if and ?ifelse, since you are not giving the if function the scalar it expects. For example, i$ID is a vector of three (identical, due to your use of split) values, so %in% will return three logical values. I think one way to do this is to replace i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID with i[ 1, "ID" ] %in% data_qual[ , "ID"] --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On September 29, 2014 4:05:54 PM PDT, Nebulo Archae <nebuloso78 at gmx.ch> wrote:>Dear all, > >I have a data.frame xy that contains numeric data and data_qual which >contains qualitative data which I want to include in a for loop with an >if statement (commented out in the code below). >The if statement should be applied if the ID in data_qual$ID is the >same than in xy$ID. > >I am trying to make it independent by doing this: > >if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){...} >? >but it doesn't work when I incorporate it in the for loop. The logical >result is correct but it only works for the first element when >implemented in the for loop. Can anyone see what I am doing wrong here? > >####################### >Here is my code: >xy ><-data.frame(NAME=c("NAME1","NAME1","NAME1","NAME2","NAME2","NAME2","NAME3","NAME3"),ID=c(87,87,87,199,199,199,233,233),X_START_YEAR=c(1950,1988,1994,1899,1909,1924,1945,1948),Y_START_VALUE=c(75,25,-90,-8,-55,-10,-9,12),X_END_YEAR=c(1985,1994,1999,1904,1924,1987,1946,1949), >Y_END_VALUE=c(20,50,-15,-70,-80,-100,24,59)) >? >data_qual <- >data.frame(NAME=c("NAME2","NAME3"),ID=c(199,233),X_START_YEAR=c(1986,1905), >Y_START_VALUE=c("-X","ST"),X_END_YEAR=c(1987,1907),Y_END_VALUE=c("-X","ST")) >? ># split xy by group as defined by ID >ind <- split(xy,xy$ID) >? >for (i in ind){xx = unlist(i[,grep('X_',colnames(i))])? >?? ? ? ? ? ? ? yy = unlist(i[,grep('Y_',colnames(i))])? >?? ? ? ? ? ? ? fname <- paste0(i[1, 'ID'], '.png')? >?? ? ? ? ? ? ? png(fname, width=1679, height=1165, res=150) >? if(any(xx < 1946)) {my_x_lim <- c(min(xx), 2014)} else {my_x_lim <- >c(1946, 2014)}? >? par(mar=c(6,8,6,5)) >? plot(xx, yy,main=unique(i[,1]),xlab="Time [Years]",ylab="Value >[m]",pch=21,xlim = my_x_lim,font.lab=2, cex.lab=1.2, cex.axis=1.1) >? i <- i[,-1] >? segments(i[,2],i[,3],i[,4],i[,5],lwd=2) >? >?# if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){ >?# ? ? rel_data_qual <- data_qual[data_qual$ID %in% i$ID,] >?# ? ? text(x = rel_data_qual$X_END_YEAR, >?#? ? ? ? ? y = min(i$Y_END_VALUE + 3), >?# ? ? labels = rel_data_qual$Y_END_VALUE) ? >?# ? } >? >? dev.off() >} > >Thanks, Kurt > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.