Open Rachelly opened 8 years ago
Yes, this how it is meant to work, although I agree it is a dangerous default behaviour.
Check the documentation for data.frame
, which recommends using match
when exact matching is desired. But you have to be careful with indexes that are in the query and not in the data.frame
: look at what x[NA, ]
returns:
x[NA, ]
> x[NA, ]
A B C
NA NA NA NA
NA.1 NA NA NA
NA.2 NA NA NA
NA.3 NA NA NA
No comment on the disastrous potential side effect of this.
Options:
matrix
object.x=data.frame("A"=c(1,2,3,4), "B"=c(1,2,3,4), "C"=c(1,2,3,4), row.names = c("123","345","1201","22"))
index <- c('123', '120', '12')
subset_row <- function(x, index){
res <- x[match(index, rownames(x), nomatch = nrow(x) + 1), ]
rownames(res) <- index
res
}
subset_row(x, index)
This really struck me.. When sub-setting a data.frame using the [] operator, partial matched names are picked! Is this how it's supposed to work?? I vaguely remember that I discussed this with Renaud once, but don't remember the conclusion we got to.. Thanks! Rachelly.
x=data.frame("A"=c(1,2,3,4), "B"=c(1,2,3,4), "C"=c(1,2,3,4), row.names = c("123","345","1201","22"))