【发布时间】:2021-01-27 01:09:40
【问题描述】:
我想在tibble 中选择名为“b”的列。 “b”是名为col_name 的变量的值。
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
col_name <- "b" # the column to select
df <- tibble(a = c(1,2,3), b = c(2,4,6))
df$b
#> [1] 2 4 6
# But I want to use a variable to dynamically select the column
df$col_name
#> Warning: Unknown or uninitialised column: `col_name`.
#> NULL
Created on 2020-10-12 by the reprex package (v0.3.0)
【问题讨论】:
-
也许
df[ , col_name ]? -
如果你想留在dplyr,那么也许
df %>% pull(!!col_name) -
当然。谢谢,对不起。
-
别担心,重复有助于将相似的内容放在一起,请不要删除。