This is one of the things that really annoys me in R. Consider the following example:
a=data.frame(x=c(1,2),y=c(3,4))
i=which(a$x==0)
At this point, i is "integer(0)" and length(i) is 0. Now if I do:
b=a[-i,]
Because I'm deleting by an empty index, I expect b to have all the data in a. But instead b is an empty data frame. I have to do this instead:
if (length(i)>0) b=a[-i,] else b=a
The same applies to matrices too. Is there a way to delete that handles empty index correctly without the if-else on my part ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…