Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get each column as data.frame (instead of a vector) from a data.frame?

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?

like image 350
RNA Avatar asked Apr 06 '12 19:04

RNA


2 Answers

use the argument drop = FALSE as in:

mtcars[, 1, drop = FALSE]
like image 88
Tyler Rinker Avatar answered Nov 10 '22 23:11

Tyler Rinker


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
like image 22
Thraupidae Avatar answered Nov 11 '22 01:11

Thraupidae