How do I extract a single column from a data.frame as a data.frame?
Omit the ,
:
x <- df[1]
A
1 10
2 20
3 30
From the help page of ?"["
:
Indexing by [ is similar to atomic vectors and selects a list of the specified element(s).
A data frame is a list. The columns are its elements.
Use drop=FALSE
> x <- df[,1, drop=FALSE]
> x
A
1 10
2 20
3 30
From the documentation (see ?"["
) you can find:
If drop=TRUE the result is coerced to the lowest possible dimension.