Normally when you get a column, it is a vector. How can I keep it as the data.frame with the same row names and corresponding column name?
use the argument drop = FALSE as in:
mtcars[, 1, drop = FALSE]
Instead of calling the desired column with a comma i.e. data.frame[,i] use data.frame[i] to preserve the class as data.frame and also retain row names.
data.frame[,i] #As a vector
data.frame[i] #As a data.frame
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With