使用dplyr重新编码多个列

D. *_*mpo 4 r dplyr recode mutate

我有一个数据框,我在其中重新编码了几列,以便将999设置为NA

dfB <-dfA %>%
  mutate(adhere = if_else(adhere==999, as.numeric(NA), adhere)) %>%
  mutate(engage = if_else(engage==999, as.numeric(NA), engage)) %>%
  mutate(quality = if_else(quality==999, as.numeric(NA), quality)) %>%
  mutate(undrstnd = if_else(undrstnd==999, as.numeric(NA), undrstnd)) %>%
  mutate(sesspart = if_else(sesspart==999, as.numeric(NA), sesspart)) %>%
  mutate(attended = if_else(attended>=9, as.integer(NA), attended))
Run Code Online (Sandbox Code Playgroud)

我想使用mutate_at()和一系列列和recode()而不是if_else(),但我仍然坚持如何给它条件.我认为999基于一些mutate_all示例的类似= NA的东西 - 但是我还需要NA来匹配.x的类型,我不确定如何使它对类型敏感

我试过了:

y <- data.frame(y1=c(1,2,999,3,4), y2=c(1L, 2L, 999L, 3L, 4L), y3=c(T,T,F,F,T))
z <- y %>%
    mutate_at( vars(y1:y2), funs(recode(.,`999` = as.numeric(NA))))
Run Code Online (Sandbox Code Playgroud)

但我得到一个警告"未被替换的值被视为NA为.x不兼容.请详细指定替换或提供.default"我可以看到它的数字列,但不是整数列y2"

> z
  y1 y2    y3
1  1 NA  TRUE
2  2 NA  TRUE
3 NA NA FALSE
4  3 NA FALSE
5  4 NA  TRUE
Run Code Online (Sandbox Code Playgroud)

ard*_*aar 7

我无法准确理解您想要完成的任务,所以如果这不完全是,请告诉我。


library(dplyr)

y <- data.frame(y1=c(1,2,999,3,4), y2=c(1L, 2L, 999L, 3L, 4L), y3=c(T,T,F,F,T))

y

#>    y1  y2    y3
#> 1   1   1  TRUE
#> 2   2   2  TRUE
#> 3 999 999 FALSE
#> 4   3   3 FALSE
#> 5   4   4  TRUE

z <- y %>%
  mutate_at(vars(y1:y2), ~ifelse(. == 999, NA, .))

z

#>   y1 y2    y3
#> 1  1  1  TRUE
#> 2  2  2  TRUE
#> 3 NA NA FALSE
#> 4  3  3 FALSE
#> 5  4  4  TRUE
Run Code Online (Sandbox Code Playgroud)


小智 7

目前,基于dplyr 文档

across() 取代了“作用域变体”系列,例如 summarise_at()、summarise_if() 和 summarise_all()。

因此,现在建议使用mutateand代替。across

就像Chris LeBoa 所说,如果您只想将一个烦人的值转换为NA,该函数na_if()可能是最好的选择:

y <- data.frame(y1=c(1,2,999,3,4), y2=c(1L, 2L, 999L, 3L, 4L), y3=c(T,T,F,F,T))

y
   y1  y2    y3
1   1   1  TRUE
2   2   2  TRUE
3 999 999 FALSE
4   3   3 FALSE
5   4   4  TRUE
 
z <- y %>%
    mutate(across(
        y1:y2,
        ~na_if(., 999)
    ))

z
  y1 y2    y3
1  1  1  TRUE
2  2  2  TRUE
3 NA NA FALSE
4  3  3 FALSE
5  4  4  TRUE
Run Code Online (Sandbox Code Playgroud)

同样,如果您确实想要recode多列中的值,您可以按照bcarothers示例进行操作:

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

df2 <- df1 %>%
    mutate(across(
        starts_with("Q8") & ends_with("TEXT"),
        ~recode(., "grogu"="mando")
    ))
Run Code Online (Sandbox Code Playgroud)


www*_*www 6

我认为它与列类型有关.我添加mutate_if将所有整数列转换为数字,然后将重新编码值设置为NA_real_.看起来很有效.

library(dplyr)

y <- data.frame(y1=c(1,2,999,3,4), y2=c(1L, 2L, 999L, 3L, 4L), y3=c(T,T,F,F,T))

z <- y %>%
  mutate_if(is.integer, as.numeric) %>%
  mutate_at(vars(y1:y2), funs(recode(.,`999` = NA_real_)))
z
#   y1 y2    y3
# 1  1  1  TRUE
# 2  2  2  TRUE
# 3 NA NA FALSE
# 4  3  3 FALSE
# 5  4  4  TRUE
Run Code Online (Sandbox Code Playgroud)


bca*_*ers 6

现在funs已在 dplyr 中折旧,这是新的方法:

z <- y %>%
  mutate_if(is.integer, as.numeric) %>%
  mutate_at(vars(y1:y2), list(~recode(.,`999` = NA_real_)))
Run Code Online (Sandbox Code Playgroud)

替换funslist并在~之前插入一个recode

  • 如果只调用一个函数,则不需要“list()”。 (2认同)