R dplyr方法变异变量(如果存在)

Fie*_*err 2 r dplyr

作为dplyr及其整洁的数据概念的忠实拥护者,我想在数据帧中存在某个特定变量时对其进行突变。这是个主意:

# Load libraries
library(dplyr)

# Create data frames
df1 <- data.frame(year = 2000:2010, foo = 0:10)
df2 <- data.frame(year = 2000:2010)

# Create function
cnd_mtt <- function(df){
  df %>%
    mutate_if(colname == "foo", as.factor) # <---- this is the tricky part
}
Run Code Online (Sandbox Code Playgroud)

预期的结果:该函数应同时适用于数据帧和无错误

有想法吗?

Psi*_*dom 5

如果该列不存在,可以使用mutate_atwith one_of引发警告消息:

cnd_mtt <- function(df){
    df %>%
        mutate_at(vars(one_of('foo')), as.factor)
}

cnd_mtt(df2)
#   year
#1  2000
#2  2001
#3  2002
#4  2003
#5  2004
#6  2005
#7  2006
#8  2007
#9  2008
#10 2009
#11 2010
Run Code Online (Sandbox Code Playgroud)
Warning message:
Unknown variables: `foo`
Run Code Online (Sandbox Code Playgroud)

只是为了澄清,当警告消息one_of无法从vars变量中解析列名时,将引发警告消息:

one_of('foo', vars = names(df1))
# [1] 2
one_of('foo', vars = names(df2))
# integer(0)
Run Code Online (Sandbox Code Playgroud)
Warning message:
Unknown variables: `foo`
Run Code Online (Sandbox Code Playgroud)

如果你想彻底摆脱了警告消息得到进一步的,采取@格里的评论,你可以使用mutate_atif/else和收益integer(0),如果foo在列不存在:

df2 %>% 
    mutate_at(if('foo' %in% names(.)) 'foo' else integer(0), as.factor)

#   year
#1  2000
#2  2001
#3  2002
#4  2003
#5  2004
#6  2005
#7  2006
#8  2007
#9  2008
#10 2009
#11 2010
Run Code Online (Sandbox Code Playgroud)