Difference using names or entries in a column selecting elements from data frame.
Use names to select elements from data frame, if possible.
When using the names, the slicing index does not need to the the same length as the data,
but if one of the column is used, the index and the data should be the same length.

Example

test.df <- data.frame(cbind(letters[1:3], 1:3, 4:6)
row.names(test.df) <- letters[1:3]
# When the length of the index is different from the number of row of the data frame, it does not work.
test.idx <- c('a', 'c')
test.df[test.idx, ]
test.df[test.df[[[1]], ]
# When the length of the index is the same as the row of the data frame, it works
test.idx <- c('a', 'b', 'c')
test.df[test.idx, ]
test.df[test.df[[[1]], ]
Difference using names or entries in a column…
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *