2018-11-04
阅读量:
940
使用tibble取子集
迄今为止,所有工具都是作用于整个数据框。如果想要提取单个变量,那么就需
要一些新工具,如 $ 和 [[。 [[ 可以按名称或位置提取变量; $ 只能按名称提取变量,但可
以减少一些输入:
df <- tibble(
x = runif(5),
y = rnorm(5)
)
# 按名称提取
df$x
#> [1] 0.434 0.395 0.548 0.762 0.254
df[["x"]]
#> [1] 0.434 0.395 0.548 0.762 0.254
# 按位置提取
df[[1]]
#> [1] 0.434 0.395 0.548 0.762 0.254
要想在管道中使用这些提取操作,需要使用特殊的占位符 .:
df %>% .$x
#> [1] 0.434 0.395 0.548 0.762 0.254
df %>% .[["x"]]
#> [1] 0.434 0.395 0.548 0.762 0.254
与 data.frame 相比, tibble 更严格:它不能进行部分匹配,如果想要访问的列不存在,它
会生成一条警告信息。






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
1条评论