0

I have a list with three xts that contains one column.

:'xts' : 250 obs. of 1 variable:
:'xts' : 245 obs. of 1 variable:
:'xts' : 250 obs. of 1 variable:

XTS second number of values is smallest.

How to balance the number of variables in this list? (It is worth remembering that the XTS has a variable as the time.)

Below is an example of how I would do:

index    time       xts1 xts2 xts3
    1    2016-10-27  10  34  7
    2    2016-10-26  41      16
    3    2016-10-25  19  60  25
    4    2016-10-24  50      92
    5    2016-10-23  34  75  6

If some values not, just delete the rows with a missing value. That is, remove the 2 and 4 row into the list.

epo3
  • 2,991
  • 2
  • 33
  • 60
Funrab
  • 83
  • 1
  • 10

3 Answers3

3

It's worth it to begin using NA for missing values and not blank spaces. For data structure purposes it matters to make this adjustment. Many R objects must be of one type. Inserting blanks causes matrices and vectors to be coerced to type "character". This is not the desired behavior if functions requiring numeric elements will be used.

You can subset an xts object just as you would a matrix:

#Example
mat
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050       NA 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347       NA 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185
2007-01-08 50.03555       NA 49.96971 49.98806
2007-01-09 49.99489 49.99489 49.80454 49.91333

We can subset as we normally would:

mat2 <- mat[!is.na( mat[,2] ),]
Pierre L
  • 28,203
  • 6
  • 47
  • 69
1

Supposing your data is in a dataframe, you could do:

df[rowSums(df[,3:5] == '') == 0,]

the result:

  index       time xts1 xts2 xts3
1     1 2016-10-27   10   34    7
3     3 2016-10-25   19   60   25
5     5 2016-10-23   34   75    6

If the missing values are represented by NAs, then you should change it to:

df[rowSums(is.na(df[,3:5])) == 0,]

The advantage of using rowSums is that you can check for missing values in multiple columns.


As @aichao noted, when you have separate xts objects, you can use merge with all = FALSE to combine them into one. However, I would use a combination of Reduce and merge (source):

xts.c <- Reduce(function(...) merge(..., all = FALSE), list(xts1,xts2,xts3))
names(xts.c) <- c('xts1','xts2','xts3')

the result:

> xts.c
           xts1 xts2 xts3
2016-10-23   34   75    6
2016-10-25   19   60   25
2016-10-27   10   34    7
Community
  • 1
  • 1
h3rm4n
  • 4,126
  • 15
  • 21
1

Since your xts objects are already in a list, the easiest thing to do is use do.call to call merge with your list. do.call allows you to pass a list of objects to merge instead of having to type all their names by hand.

library(xts)
# example data
dates <- seq(as.Date("2016-10-23"), as.Date("2016-10-27"), by = "day")
xtslist <- list(
  xts1 = xts(c(34, 50, 19, 41, 10), dates),
  xts2 = xts(c(75, 60, 34), dates[c(1, 3, 5)]),
  xts3 = xts(c(6, 92, 25, 16, 7), dates))
# merge all list elements into a single xts object
# Note: do.call(merge, xtslist) is equivalent to
#       merge(xtslist[[1]], xtslist[[2]], xtslist[[3]])
(myxts <- do.call(merge, xtslist))
#            xts1 xts2 xts3
# 2016-10-23   34   75    6
# 2016-10-24   50   NA   92
# 2016-10-25   19   60   25
# 2016-10-26   41   NA   16
# 2016-10-27   10   34    7

If you want to omit the rows with NA, you can either call na.omit(myxts), or you can use the all = FALSE argument to merge.xts. Here's how you would specify all = FALSE in your do.call command.

(myxts2 <- do.call(merge, c(xtslist, all = FALSE)))
#            xts1 xts2 xts3
# 2016-10-23   34   75    6
# 2016-10-25   19   60   25
# 2016-10-27   10   34    7
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418