问题描述
如果我使用df$columnName
,则只会得到一个不再具有该列名称的向量。
这意味着names(df$columnName) --> null
我可以使用df["columnName"]
,但这有点麻烦。.因为我必须将columnName作为字符串字符传递。
解决方法
我更喜欢dplyr
的{{1}}。
select()
返回一列数据框。
,在答案中形成 @Edo 的精彩评论,我们可以使用subset
。
subset(x=dat,subset=,select=X1)
或简称:
subset(dat,X1)
# X1
# 1 1
# 2 2
# 3 3
数据:
dat <- structure(list(X1 = 1:3,X2 = 4:6,X3 = 7:9,X4 = 10:12),class = "data.frame",row.names = c(NA,-3L))