R: How to add missing dates to a dataframe twofold and and give each a different value in other column? -


i have following dataframe part of larger one:

[7080,] 20100303 3.669138e-01  0.000000000 [7081,] 20100303 4.347603e-01  0.000000000 [7082,] 20100305 4.252109e-01  0.000000000 [7083,] 20100306 3.865164e-01  0.000000000 [7084,] 20100307 2.799683e-01  0.000000000 [7085,] 20100307 3.478009e-01  0.000000000 [7086,] 20100309 3.381812e-01  0.000000000 

as can see first column consists of dates of have 2 measurements , others have 1. dates missing.

if date missing want create 2 different "measurements" particular date, 1 value "0" , 1 value "1" in second column (i want value "1" above "0"). value of third column has 99 (not na). result this:

[7080,] 20100303 3.669138e-01  0.000000000 [7081,] 20100303 4.347603e-01  0.000000000         20100304 1             99         20100304 0             99 [7082,] 20100305 4.252109e-01  0.000000000 [7083,] 20100306 3.865164e-01  0.000000000 [7084,] 20100307 2.799683e-01  0.000000000 [7085,] 20100307 3.478009e-01  0.000000000         20100308 1             99         20100308 0             99 [7086,] 20100309 3.381812e-01  0.000000000 

does know how this? awesome!

you don't give variable names made up:

# alldates       # vector of possible dates # currentdates   # current dataframe variable of dates 

to alldates, might try generating range of possible dates origin of timeseries point in future. example following gives first 10 days starting after 1900-01-01:

as.date(1:10,origin = "1900-01-01") 

with in hand, should relatively easy you're requesting:

# generate vector of missing dates z1 <- rep(alldates[!alldates %in% currentdates],each=2)  # generate vector of 0's , 1's z2 <- rep(c(1,0),(length(z1)/2))  # generate vector of 99's z3 <- rep(99,length(z2))  cbind(z1,z2,z3) # put 

you can add original data using rbind or whatever.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -