使用dplyr替换某些data.frame列中的值

时间:2018-06-21 12:36:44

标签: r dplyr

我想用0替换data.frame中的1,但仅在因子列中使用,该列只有3个值(0、1或NA)。我必须避免也按名称指定列,因为我的真实数据集很大,而且很麻烦。所以我想我可以利用dplyr::mutate_if并尝试类似的事情:

df %>% mutate_if(~(is.factor(.) & (unique(.) %in% c(0, 1, NA))), ~replace(., . == 0, 1))

但最终出现以下错误:

  

selected [[i]] <-.p(.tbl [[vars [[i]]]],...)中的错误:     提供的元素多于要替换的元素

此公式有什么问题?如何使用dplyr将0替换为1? 我的示例数据集如下所示:

df <- structure(list(a1 = structure(c(1L, NA, NA, 2L, NA, 1L, NA), .Label = c("0", 
"1"), class = "factor"), a2 = structure(c(NA, NA, NA, 1L, NA, 
NA, NA), .Label = "1", class = "factor"), a3 = structure(c(NA, 
1L, 2L, 3L, NA, 4L, 2L), .Label = c("0", "1", "2", "6"), class = "factor"), 
a4 = structure(c(1L, 1L, NA, NA, NA, NA, 1L), .Label = "0", class = 
"factor"), 
a5 = c(0L, 1L, 1L, NA, 1L, 0L, NA)), .Names = c("a1", "a2", 
"a3", "a4", "a5"), class = c("tbl_df", "tbl", "data.frame"), row.names = 
c(NA, -7L))

2 个答案:

答案 0 :(得分:0)

怎么样?

df %>%
    mutate_if(is.factor, funs(ifelse(as.character(.) == "0", "1", as.character(.)))) %>%
    mutate_if(is.character, as.factor)
## A tibble: 7 x 5
#  a1    a2    a3    a4       a5
#  <fct> <fct> <fct> <fct> <int>
#1 1     NA    NA    1         0
#2 NA    NA    1     1         1
#3 NA    NA    1     NA        1
#4 1     1     2     NA       NA
#5 NA    NA    NA    NA        1
#6 1     NA    6     NA        0
#7 NA    NA    1     1        NA

答案 1 :(得分:0)

可以这样解决:

df %>%
mutate_if(~(is.factor(.) & (all(unique(.) %in% c(0, 1, NA)))), ~plyr::revalue(., c("0"="1")))

# # A tibble: 7 x 5
#   a1    a2    a3    a4       a5
#   <fct> <fct> <fct> <fct> <int>
# 1 1     <NA>  <NA>  1         0
# 2 <NA>  <NA>  0     1         1
# 3 <NA>  <NA>  1     <NA>      1
# 4 1     1     2     <NA>     NA
# 5 <NA>  <NA>  <NA>  <NA>      1
# 6 1     <NA>  6     <NA>      0
# 7 <NA>  <NA>  1     1        NA