r 使用dplyr按名称向量选择列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 使用dplyr按名称向量选择列相关的知识,希望对你有一定的参考价值。

one <- seq(1:10)
two <- rnorm(10)
three <- runif(10, 1, 2)
four <- -10:-1

df <- data.frame(one, two, three)
df2 <- data.frame(one, two, three, four)

str(df)

names.df <- colnames(df)
names.df.2 <- c("one", "two", "three")

#install.packages("dplyr")
library(dplyr)

select_(df2, names.df)           # no - only first variable name
select_(df2, names.df.2)         # no - only first variable name
select(df2, one_of(names.df))    # success
select(df2, one_of(names.df.2))  # success

以上是关于r 使用dplyr按名称向量选择列的主要内容,如果未能解决你的问题,请参考以下文章