One common task when manipulating data in R is to bind to a data frame by deleting rows that match certain criteria. However, a simple way to do this in R seems logically inconsistent and even dangerous for the inexperienced (like me).
Suppose we have a data frame, and we want to exclude lines related to processing "G1":
Treatment=c("G1","G1","G1","G1","G1","G1","G2","G2","G2","G2","G2", "G2","G3","G3","G3","G3","G3","G3") Vals=c(runif(6),runif(6)+0.9,runif(6)-0.3) data=data.frame(Treatment) data=cbind(data, Vals)
As expected, the code below deletes rows of data that match the criteria of the first row
to_del=which(data$Treatment=="G1") new_data=data[-to_del,] new_data
However, contrary to what is expected, using this approach, if the βwhichβ command does not find ANY matching line, this code deletes all the lines, and does not leave them alone
to_del=which(data$Treatment=="G4") new_data=data[-to_del,] new_data
The above code leads to a data frame without any remaining rows, which makes no sense (i.e., since R does not detect rows that match my criteria for deletion, it deletes all rows). My work works, but I would suggest that there is an easier way to do this without all these conditional statements
###WORKAROUND to_del=which(data$Treatment=="G4") #no G4 treatment in this particular data frame if (length(to_del)>0){ new_data=data[-to_del,] }else{ new_data=data } new_data
Does anyone have an easy way to do this that works, even if none of the lines match the specified criteria?