Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用dplyr::mutate over在函数中不工作时重新编码_R_Dplyr_Tidyr_Mutate_Across - Fatal编程技术网

使用dplyr::mutate over在函数中不工作时重新编码

使用dplyr::mutate over在函数中不工作时重新编码,r,dplyr,tidyr,mutate,across,R,Dplyr,Tidyr,Mutate,Across,我试图使用dplyr::mutate(cross())在tbl中重新编码指定的列。单独使用这些功能很好,但我无法让它们在函数中工作: library(dplyr) library(tidyr) df1 <- tibble(Q7_1=1:5, Q7_1_TEXT=c("let's","see","grogu","this","week"),

我试图使用
dplyr::mutate(cross())
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")
库(dplyr)
图书馆(tidyr)

df1的一种方法是使用
~重新编码(,!!!setNames(newTx,oldTx))
@Axeman,这很有效!如果你想添加一个答案,我会接受的。谢谢@阿克塞曼,你能告诉我在哪里可以学到更多的“!!”吗