我正在尝试使用dplyr::mutate(across())
重新编码 a 中的指定列tbl
。单独使用它们效果很好,但我无法让它们在函数中工作:
library(dplyr)
library(tidyr)
df1 <- tibble(Q7_1=1:5,
Q7_1_TEXT=c("let's","see","grogu","this","week"),
Q8_1=6:10,
Q8_1_TEXT=rep("grogu",5),
Q8_2=11:15,
Q8_2_TEXT=c("grogu","is","the","absolute","best"))
# this works
df2 <- df1 %>%
mutate(across(starts_with("Q8") & ends_with("TEXT"),
~recode(., "grogu"="mando")))
# runs without error, but doesn't perform the recode
clnFun <- function(dat, oldTx, newTx){
df <- dat %>%
mutate(across(starts_with("Q8") & ends_with("TEXT"),
~recode(., oldTx=newTx)
)
)
}
df3 <- clnFun(df1, "grogu", "mando")
我缺少什么?