Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从data.frame中提取单个列作为data.frame?_R_Dataframe_Subset - Fatal编程技术网

如何从data.frame中提取单个列作为data.frame?

如何从data.frame中提取单个列作为data.frame?,r,dataframe,subset,R,Dataframe,Subset,假设我有一个data.frame: df <- data.frame(A=c(10,20,30),B=c(11,22,33), C=c(111,222,333)) A B C 1 10 11 111 2 20 22 222 3 30 33 333 x <- df[,1:2] A B 1 10 11 2 20 22 3 30 33 df使用drop=FALSE > x <- df[,1, drop=FALSE] > x A 1 10 2

假设我有一个data.frame:

df <- data.frame(A=c(10,20,30),B=c(11,22,33), C=c(111,222,333))
  A  B  C
1 10 11 111
2 20 22 222
3 30 33 333
x <- df[,1:2]
   A  B
 1 10 11
 2 20 22
 3 30 33

df使用
drop=FALSE

> x <- df[,1, drop=FALSE]
> x
   A
1 10
2 20
3 30
>x
A.
1 10
2 20
3 30
从文档(请参阅)中,您可以找到:

如果drop=TRUE,则结果强制为可能的最低维度


省略

x <- df[1]

   A
1 10
2 20
3 30

x这不是重复,因为matrix和data.frame与dplyr的工作方式不同。对于data.frame,dplyr的整洁方式是:选择:
mtcars%>%dplyr::选择(“wt”)
> x <- df[,1, drop=FALSE]
> x
   A
1 10
2 20
3 30
x <- df[1]

   A
1 10
2 20
3 30