Reputation: 1153
This one has me tearing my hair out. Suppose I have a data frame like this:
xx <- data.frame(A = c(1,2,3), B = c(4,5,6))
rownames(xx) <- c("p", "q", "r")
This gives:
A B
p 1 4
q 2 5
r 3 6
I have R code that can result in an empty character (character(0)) that is used to try and subset df xx. If I define a zero character:
z = character(0)
and then I subset xx with:
subset(xx, rownames(xx) != z)
I get:
[1] A B
<0 rows> (or 0-length row.names)
but what I want is the full array, xx, since there is no empty row name. To make this work I have to convert the empty character to and empty string:
z <- ""
Using z <- "", when I subset I get the entire df back, which is what I expect. Explicitly trapping and converting all of these empty characters to empty strings is too clumsy. How to solve this??
Upvotes: 0
Views: 575
Reputation: 3310
Try using %in%
instead of !=
:
subset(xx, !(rownames(xx) %in% z))
Upvotes: 1