2017年2月17日金曜日

Manipulate columns

When data day_xts has multiple columns and you like to delete or pick up one of them, writing a simple condition clause is enough to do that job.
However, in the case to deal with more than one column, it doesn't work and you need other solution.


> head(day_xts)
           new_call new_mail abandon inc
2013-01-01       35       28    12.9  63
2013-01-02       55       21    31.1  76
2013-01-03       86       20    76.7 106


> head(day_xts[,colnames(day_xts) != "inc"])
           new_call new_mail abandon
2013-01-01       35       28    12.9
2013-01-02       55       21    31.1
2013-01-03       86       20    76.7


Using "grep" with regular expression picks up columns either "inc" or "abandon".

> head(day_xts[,grep('inc|abandon',colnames(day_xts))])
           abandon inc
2013-01-01    12.9  63
2013-01-02    31.1  76
2013-01-03    76.7 106


"invert" argument does reverse the result. The columns which are NOT designated in "grep" will be chosen.

> head(day_xts[,grep('inc|abandon',colnames(day_xts),invert=T)])
           new_call new_mail
2013-01-01       35       28
2013-01-02       55       21
2013-01-03       86       20

0 件のコメント: