Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rowSums but keeping NA values

Tags:

r

xts

I am pretty sure this is quite simple, but seem to have got stuck...I have two xts vectors that have been merged together, which contain numeric values and NAs.

I would like to get the rowSums for each index period, but keeping the NA values.

Below is a reproducible example

set.seed(120)
dd <- xts(rnorm(100),Sys.Date()-c(100:1))
dd1 <- ifelse(dd<(-0.5),dd*-1,NA)
dd2 <- ifelse((dd^2)>0.5,dd,NA)
mm <- merge(dd1,dd2)
mm$m <- rowSums(mm,na.rm=TRUE)
tail(mm,10)

                 dd1        dd2        m
2013-08-02        NA         NA 0.000000
2013-08-03        NA         NA 0.000000
2013-08-04        NA         NA 0.000000
2013-08-05 1.2542692 -1.2542692 0.000000
2013-08-06        NA  1.3325804 1.332580
2013-08-07        NA  0.7726740 0.772674
2013-08-08 0.8158402 -0.8158402 0.000000
2013-08-09        NA  1.2292919 1.229292
2013-08-10        NA         NA 0.000000
2013-08-11        NA  0.9334900 0.933490

In the above example on the 10th Aug 2013 I was hoping it would say NA instead of 0, the same goes for the 2nd-4th Aug 2013.

Any suggestions for an elegant way of getting NAs in the relevant places?

like image 800
h.l.m Avatar asked Aug 12 '13 14:08

h.l.m


1 Answers

If you have a variable number of columns you could try this approach:

mm <- merge(dd1,dd2)
mm$m <- rowSums(mm, na.rm=TRUE) * ifelse(rowSums(is.na(mm)) == ncol(mm), NA, 1)
# or, as @JoshuaUlrich commented:
#mm$m <- ifelse(apply(is.na(mm),1,all),NA,rowSums(mm,na.rm=TRUE))
tail(mm, 10)
#                  dd1        dd2        m
#2013-08-02        NA         NA       NA
#2013-08-03        NA         NA       NA
#2013-08-04        NA         NA       NA
#2013-08-05 1.2542692 -1.2542692 0.000000
#2013-08-06        NA  1.3325804 1.332580
#2013-08-07        NA  0.7726740 0.772674
#2013-08-08 0.8158402 -0.8158402 0.000000
#2013-08-09        NA  1.2292919 1.229292
#2013-08-10        NA         NA       NA
#2013-08-11        NA  0.9334900 0.933490
like image 169
sgibb Avatar answered Sep 22 '22 00:09

sgibb