我试图将12小时后包含单词“length”的变量的所有值更改为NA。
df_data <- cbind(
seq(0, 15, by = 0.5),
sample(seq(from = 100, to = 300, by = 10), size = 31, replace = TRUE),
sample(seq(from = 1, to = 100, by = 9), size = 31, replace = TRUE),
sample(seq(from = 50, to = 60, by = 2), size = 31, replace = TRUE),
sample(seq(from = 100, to = 130, by = 1), size = 31, replace = TRUE)
) %>% as.data.frame()
colnames(df_data) <- c("hour", "a", "a_lenght", "b", "b_length")
df_new <- df_data %>%
mutate_if(vars(contains("length")), funs(ifelse(df_data$hour > 12, NA, .)))
但我收到Can't convert a list to function
错误
答案 0 :(得分:7)
总结已经提出的意见:
mutate_at使用列名 - 当您可以提供按列名特征选择列的条件时,它最有用。 这通常采取以下形式: mutate_at(vars(ends_with(&#34; length&#34;)),funs(...))
mutate_if使用列内容 - 在提供基于列本身内容选择列的谓词函数时最有用。这通常采取以下形式: mutate_if(is.integer,funs(...))
但是正如加林娜指出的那样,mutate_if是多才多艺的,并且可以被欺骗&#34;使用grepl
操作列名