使用 magrittr 有条件地替换值

Conditionally replace values using magrittr

假设我有一个要对其执行转换的数据框。 通常它看起来像:

a <- data.frame(c(NA, 0,1), c(34,NA,0), c(3,9,NA) )
b <- c('key1', 'key2', 'key3')
####replace NA values with 0
a[is.na(a)] <- 0
####replace 1 with 2
a[a==1] <- 2
####sum rows
a <- rowSums(a)
####bind b as key column for joining datasets in a later stage
c <- cbind(b, a)

现在我的问题是:如何将其翻译成 magrittr

library(magrittr)
c %>% 
.[is.na] %>% 0 %>% .[.==1] %>% 2 %>%
rowSums %>% cbind(b, .)

给我:

Error in .[is.na(.)] : object of type 'builtin' is not subsettable
In addition: Warning message:
In is.na(.) : is.na() applied to non-(list or vector) of type 'builtin'

我们可以使用dplyr

library(dplyr)
a %>%
    mutate_each(funs(replace(., is.na(.), 0))) %>% 
    mutate_each(funs(replace(., .==1, 2))) %>%
    rowSums(.) %>%
    data_frame(key = b, val = .)
#    key   val
#   <chr> <dbl>
#1  key1    37
#2  key2     9
#3  key3     2

或者不使用 dplyr 函数

 a %>% 
    is.na(.) %>%
    replace(a, ., 0) %>%
    replace(., .==1, 2) %>%
    rowSums() %>% 
    cbind(b, .)

比@akrun 建议的稍微快一点的方法(在打字方面,不确定在计算方面是否也更快)是使用 sjmisc 包中的 rec 函数:

library(sjmisc)
library(dplyr)

a <- data.frame(c(NA, 0,1), c(34,NA,0), c(3,9,NA) )
b <- c('key1', 'key2', 'key3')

a %>% 
  rec("NA=0;1=2;else=copy") %>% 
  rowSums(.) %>% 
  data_frame(key = b, val = .)

# A tibble: 3 x 2
#     key   val
#   <chr> <dbl>
# 1  key1    37
# 2  key2     9
# 3  key3     2