Dplyr: select all variables except those contained in the vector

This should be a simple question, but I'm trying

I have a variable name vector that I want to exclude from the data frame:

df <- data.frame(matrix(rexp(50),nrow = 10,ncol = 5))
names(df) <- paste0(rep("variable_",5),1:5)

excluded_vars <- c("variable_1","variable_3")

I thought I could just use - to exclude objects from the select statement:

select(df,-excluded_vars)

But I received the following error:

Using Select_ The same is true for ()

Any ideas?

Solution

You need to use one_ Of function:

select(df,-one_of(excluded_vars))

For more information about selecting based on variable names, see select helpers. In the dplyr documentation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>