删除R中多个变量中的字符串

Remove string in multiple variables in R

这是我的数据的 MWE,我想从中删除所有包含“Med”的列中的字符串“NaN”

df= data.frame(id= rep(1:5, each=1),
               Med1 = c("GN", "GN", "Ca", "Ca", "DM"),
               Med2 = c("DM", "NaN", "Mob", "NaN", "NaN"),
               Med3 = c("NaN","NaN","DM", "NaN","NaN"))

我试过以下方法:

dfx = df%>%
  mutate(across(contains("Med", ignore.case = TRUE), str_remove(.,"NaN")))
Error: Problem with `mutate()` input `..1`.
x Problem with `across()` input `.fns`.
i Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
Problem with `mutate()` input `..1`.
i argument is not an atomic vector; coercing
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`. 
dfx = df%>%
  mutate(across(contains("Med", ignore.case = TRUE), str_remove("NaN")))
Error: Problem with `mutate()` input `..1`.
x argument "pattern" is missing, with no default
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.

我也有一个问题,只是从一个列中删除字符串,所以我想我可能误解了 str_remove

dfy=df%>%
 str_remove(string = Med1, pattern = "NaN")
Error in str_remove(., string = Med1, pattern = "NaN") : 
  unused argument (.)

前面:在您的代码中添加波浪号:

dfx = df%>%                                        # ,--- add this tilde
  mutate(across(contains("Med", ignore.case = TRUE), ~ str_remove(.,"NaN")))

解释:across将一个函数作为它的第二个参数。这可以用几种方式表达:

  1. 原始函数,例如across(everything(), mean)。您可以在之后添加任意 named/unnamed 个参数,尽管它们与数据本身是分开的。

    mtcars %>%
      mutate(across(everything(), mean))
    mtcars %>%
      mutate(across(everything(), mean, na.rm = TRUE))
    

    (这不假定 base-R 函数:您可以在别处创建自己的函数并将其传递到此处。)

  2. 匿名函数,调用更灵活。也许:

    mtcars %>%
      mutate(across(everything(), function(z) mean(x)))
    mtcars %>%
      mutate(across(everything(), function(z) mean(x, na.rm = TRUE)))
    
  3. rlang 风格波浪线函数。其中,. 被数据向量替换(每列为 mutated):

    mtcars %>%
      mutate(across(everything(), ~ mean(.)))
    mtcars %>%
      mutate(across(everything(), ~ mean(., na.rm = TRUE)))
    

当然,您不需要stringr来完成这个任务。

df
#   id Med1 Med2 Med3
# 1  1   GN   DM  NaN
# 2  2   GN  NaN  NaN
# 3  3   Ca  Mob   DM
# 4  4   Ca  NaN  NaN
# 5  5   DM  NaN  NaN
df[df == "NaN"] <- ""
df
#   id Med1 Med2 Med3
# 1  1   GN   DM     
# 2  2   GN          
# 3  3   Ca  Mob   DM
# 4  4   Ca          
# 5  5   DM