Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing NA columns in xts

Tags:

r

xts

quantmod

I have an xts in the following format

                   a        b     c        d       e        f   ......
2011-01-03         11.40    NA    23.12    0.23    123.11   NA  ......
2011-01-04         11.49    NA    23.15    1.11    111.11   NA  ......
2011-01-05         NA       NA    23.11    1.23    142.32   NA  ......
2011-01-06         11.64    NA    39.01    NA      124.21   NA  ......
2011-01-07         13.84    NA    12.12    1.53    152.12   NA  ......

Is there a function I can apply to generate a new xts or data.frame missing the columns containing only NA?

The position of the columns with the NAs isn't static so just removing those columns by name or position isn't possible

like image 223
lab_notes Avatar asked Oct 27 '12 09:10

lab_notes


1 Answers

Supose DF is your data.frame

 DF [, -which(sapply(DF, function(x) sum(is.na(x)))==nrow(DF))]
               a     c    d      e
2011-01-03 11.40 23.12 0.23 123.11
2011-01-04 11.49 23.15 1.11 111.11
2011-01-05    NA 23.11 1.23 142.32
2011-01-06 11.64 39.01   NA 124.21
2011-01-07 13.84 12.12 1.53 152.12
like image 104
Jilber Urbina Avatar answered Oct 11 '22 12:10

Jilber Urbina