问题
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