Reputation: 399
Trying to generate a third variable, called "var", to the dataset that only has a value of 100 for the date "2010-09-24" and NA for all other time periods.
> dataset
weight
2010-10-04 52495
2010-10-01 53000
2010-09-30 52916
2010-09-29 52785
2010-09-28 53348
2010-09-27 52885
2010-09-24 52174
2010-09-23 51461
2010-09-22 51286
2010-09-21 50968
2010-09-20 49250
> dataset=merge(dataset,var=NA)
I know I could I use the ifelse(index(dataset)=="2010-09-24",100,NA)
to generate the variable. But are there any functions that one could employ to restrict the sample only to "2010-09-24" and then place the value in that right column and row?
Upvotes: 1
Views: 648
Reputation: 457
I haven't seen merge() used this way before, and the command isn't working for me. So maybe I'm misunderstanding the question or missing something about timeseries data, but I'd do this:
1: add a new vector of NAs
dataset$var<-NA
2: index to the date you want and save the value of 100 to the variable var
dataset[dataset$date=="24-09-2010","var"]<-100
Is that what you're looking for?
Upvotes: 2
Reputation: 368181
Yes -- If you use a data type with proper time indexing such as zoo or xts.
Have a look here at SO at previous questions on these two, and/or read the vignettes in the zoo package.
Upvotes: 1