【发布时间】:2016-11-17 05:06:25
【问题描述】:
假设我有一个要对其执行转换的数据框。 通常它看起来像:
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, .)
给我:
.[is.na(.)] 中的错误:“内置”类型的对象不是子集
另外:警告信息:
在 is.na(.) 中:is.na() 应用于“内置”类型的非(列表或向量)
【问题讨论】:
标签: r replace conditional magrittr