【发布时间】:2015-02-19 00:12:20
【问题描述】:
对于普通 R 用户来说,这可能是一个非常简单的问题,但我似乎找不到解决方案。 我想添加两个缺少数据的变量。
x1<-c(NA,3,NA,5)
x2<-c(NA,NA,4,3)
x3<-x1+x2
x3
[1] NA NA NA 8
但我真正想要的是:
[1] NA 3 4 8
任何建议将不胜感激。我怎样才能保留 NA?
【问题讨论】:
标签: r missing-data
对于普通 R 用户来说,这可能是一个非常简单的问题,但我似乎找不到解决方案。 我想添加两个缺少数据的变量。
x1<-c(NA,3,NA,5)
x2<-c(NA,NA,4,3)
x3<-x1+x2
x3
[1] NA NA NA 8
但我真正想要的是:
[1] NA 3 4 8
任何建议将不胜感激。我怎样才能保留 NA?
【问题讨论】:
标签: r missing-data
如果两者都是NA,则保留NA(抄袭@Ben Bolker 使用cbind 的方法):
apply(cbind(x1, x2), 1, function(x) ifelse(all(is.na(x)), NA, sum(x, na.rm=T)))
# [1] NA 3 4 8
或者,如果您更喜欢使用 rowSums 函数(这很有吸引力,因为它是矢量化的,而 apply 和 mapply 解决方案不是):
rowSums(cbind(x1, x2), na.rm=T) + ifelse(is.na(x1) & is.na(x2), NA, 0)
# [1] NA 3 4 8
这些都不会像 Rcpp 函数一样快(只需循环两个输入一次):
library(Rcpp)
sum.na.ign <- cppFunction("
NumericVector sumNaIgn(NumericVector x, NumericVector y) {
const int n = x.size();
NumericVector out(n);
for (int i=0; i < n; ++i) {
if (R_IsNA(x[i])) {
out[i] = y[i];
} else if (R_IsNA(y[i])) {
out[i] = x[i];
} else {
out[i] = x[i] + y[i];
}
}
return out;
}")
sum.na.ign(x1, x2)
# [1] NA 3 4 8
我们可以对更大的向量进行基准测试(以及基于@J. Won. 的mapply 的解决方案):
# First two functions along with mapply-based solution from @J. Won.
f1 <- function(x1, x2) apply(cbind(x1, x2), 1, function(x) ifelse(all(is.na(x)), NA, sum(x, na.rm=T)))
f2 <- function(x1, x2) rowSums(cbind(x1, x2), na.rm=T) + ifelse(is.na(x1) & is.na(x2), NA, 0)
NAsum <- function(...) {
if(any(!is.na(c(...)))) return(sum(..., na.rm=TRUE))
return(NA)
}
jwon <- function(x1, x2) mapply(NAsum, x1, x2)
set.seed(144)
x1 <- sample(c(NA, 1:10), 10000, replace=T)
x2 <- sample(c(NA, 1:10), 10000, replace=T)
all.equal(jwon(x1, x2), f1(x1, x2), f2(x1, x2), sum.na.ign(x1, x2))
# [1] TRUE
library(microbenchmark)
microbenchmark(jwon(x1, x2), f1(x1, x2), f2(x1, x2), sum.na.ign(x1, x2))
# Unit: microseconds
# expr min lq mean median uq max neval
# jwon(x1, x2) 24044.658 28387.4280 35580.3434 35134.9940 38175.661 91476.032 100
# f1(x1, x2) 37516.769 46664.6390 52293.5265 51570.2690 56647.063 77576.091 100
# f2(x1, x2) 2588.820 2738.0740 2930.4106 2833.4880 2974.745 5187.684 100
# sum.na.ign(x1, x2) 97.988 109.8575 132.9849 123.0795 142.725 533.275 100
rowSums 解决方案是矢量化的,因此比 apply 和 mapply 解决方案更快(对于长度为 100 万的矢量,这些解决方案会感觉很慢),但自定义 Rcpp 解决方案比 rowSums 快 10 倍以上方法。与 rowSums 相比,您的向量可能需要非常大才能使 Rcpp 有用。
【讨论】:
mapply(sum, x1, x2, na.rm=TRUE)
编辑:如果我们想要评论中要求的更复杂的版本,我认为它需要一个自定义函数
NAsum <- function(...) {
if(any(!is.na(c(...)))) return(sum(..., na.rm=TRUE))
return(NA)
}
mapply(NAsum, x1, x2)
【讨论】:
+ 没有选项可以抑制 NA 值,但您可以:
rowSums(cbind(x1,x2),na.rm=TRUE)
## [1] 2 3 4 8
【讨论】:
我尝试使用上面给出的以下代码来解决我在一个数据框中汇总两个以上变量的问题。我不确定平台是否允许这样做,但仍想分享。
应用(cbind(x1, x2), 1, function(x) ifelse(all(is.na(x)), NA, sum(x, na.rm=T)))
以下是我的数据
x1 x2 x3 VAt
1 NA NA a NA
2 3 NA b 1
3 NA 4 c 2
4 5 3 d NA
一个
One$use
One$use1
以下是输出。
X1 x1 x2 x3 VAt use use1
1 1 NA NA a NA NA NA
2 2 3 NA b 1 4 4
3 3 NA 4 c 2 6 6
4 4 5 3 d NA 8 8
感谢@swhusky 的提问和@josliber 的回答。
【讨论】: