recode using dplyr::mutate across not working in a function

不羁的心 提交于 2021-01-05 07:16:10

问题


I'm trying to use dplyr::mutate(across()) to recode specified columns in a tbl. Using these on their own works fine, but I can't get them to work in a function:

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")

What am I missing?

来源:https://stackoverflow.com/questions/65203421/recode-using-dplyrmutate-across-not-working-in-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!