rowSums but keeping NA values

前端 未结 4 506
谎友^
谎友^ 2020-12-07 02:34

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 wo

4条回答
  •  情书的邮戳
    2020-12-07 02:57

    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
    

提交回复
热议问题