2

我正在尝试使用dplyr::mutate(across())重新编码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")

我错过了什么?

4

0 回答 0