Home > Software engineering >  combining 2 series and dropping NA in R
combining 2 series and dropping NA in R

Time:10-20

I am trying to combing the dividend history of 2 different stocks and I have the below code:

library(quantmod)
library(tidyr)
AAPL<-round(getDividends("AAPL"),4)
MSFT<-round(getDividends("MSFT"),4)
dividend<-(cbind(AAPL,MSFT))

As the 2 stocks pay out dividend on different dates so there will be NAs after combining and so I try to use drop_na function from tidyr like below:

drop_na(dividend)

#Error in UseMethod("drop_na") : no applicable method for 'drop_na' applied to an object of class "c('xts', 'zoo')"

May I know what did I do wrong here? Many thanks for your help.

Update 1:

Tried na.omit, which returns with the following:

> dividend<-(cbind(AAPL,MSFT))%>%
>  na.omit(dividend)%>%
>  print(dividend)
     [,1] [,2]

CodePudding user response:

drop_na only works on data frames but even if you converted it to data frame it would not give what you want. Every row contains an NA so drop_na and na.omit would both remove every row leaving a zero row result.

Instead try aggregating over year/month which gives zoo object both. If you need an xts object use as.xts(both) .

both <- aggregate(dividend, as.yearmon, sum, na.rm = TRUE)

Optionally we could remove the rows with zero dividends in both columns:

both[rowSums(both) > 0]

CodePudding user response:

We could use na.omit

na.omit(dividend)

drop_na expects a data.frame as input whereas the the object dividend is not a data.frame

We could fortify into a data.frame and then use drop_na after reordering the NAs

library(dplyr)
library(lubridate)
fortify.zoo(dividend) %>% 
   group_by(Index = format(ymd(Index), '%Y-%b')) %>% 
   mutate(across(everything(),  ~ .x[order(is.na(.x))])) %>%
   ungroup %>%
   drop_na()

-output

# A tibble: 41 × 3
   Index    AAPL.div MSFT.div
   <chr>       <dbl>    <dbl>
 1 2012-Aug   0.0034     0.2 
 2 2012-Nov   0.0034     0.23
 3 2013-Feb   0.0034     0.23
 4 2013-May   0.0039     0.23
 5 2013-Aug   0.0039     0.23
 6 2013-Nov   0.0039     0.28
 7 2014-Feb   0.0039     0.28
 8 2014-May   0.0042     0.28
 9 2014-Aug   0.0294     0.28
10 2014-Nov   0.0294     0.31
# … with 31 more rows
  • Related