Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Use logical indexing with [ and is.na(·) to localize the entries where both are NA and then replace them with NA.

Try this:

> mm[is.na(mm$dd1) & is.na(mm$dd2), "m"] <- NA
> mm
                 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
mm$m <- "is.na<-"(rowSums(mm, na.rm = TRUE), !rowSums(!is.na(mm)))

> tail(mm)
#                  dd1        dd2        m
# 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

My solution would be

library(magrittr)
mm <- mm %>% 
      transform(ccardNA = rowSums(!is.na(.))/rowSums(!is.na(.)), m = rowSums(., na.rm = TRUE)) %>%
      transform(m = ifelse(is.nan(ccardNA), NA, m), ccardNA = NULL) %>%
      as.xts()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top