[R] Add a new row based on test set predicted values and time stamps

Elahe chalabi ch@|@b|@e|@he @end|ng |rom y@hoo@de
Tue Apr 13 12:26:22 CEST 2021


Hi all,

I have the prediction for my test set which are forecasted Value for "4/1/2020" for each match of "id" and "Group". I would like to add a fourth row to each group by (Group,id) in my train set and the values for this row should come from test set :

my train set: 

     structure(list(Date = c("1/1/2020", "2/1/2020", "3/1/2020", "1/1/2020", 
     "2/1/2020", "3/1/2020", "1/1/2020", "2/1/2020", "3/1/2020", ""
     ), Value = c(3.5, 2.7, 4, 2.5, 3.7, 0, 3, 0, 1, NA), Group = c("A", 
    "A", "A", "B", "B", "B", "C", "C", "C", ""), id = c(1L, 1L, 1L, 
    101L, 101L, 101L, 100L, 100L, 100L, NA)), class = "data.frame", row.names = c(NA, 
    -10L))

test set:

    structure(list(Date = c("4/1/2020", "4/1/2020", "4/1/2020", ""
      ), Value = c(3.5, 2.5, 3, NA), Group = c("A", "B", "C", ""), 
    id = c(1L, 101L, 100L, NA), value = c(0.2, 0.7, 0.9, NA)), class = "data.frame", row.names = c(NA, 
     -4L))structure(list(Date = c("4/1/2020", "4/1/2020", "4/1/2020", ""
    ), Value = c(3.5, 2.5, 3, NA), Group = c("A", "B", "C", ""), 
    id = c(1L, 101L, 100L, NA)), class = "data.frame", row.names = c(NA, 
    -4L))

desired output: 

    structure(list(Date = c("1/1/2020", "2/1/2020", "3/1/2020", "4/1/2020", 
    "1/1/2020", "2/1/2020", "3/1/2020", "4/1/2020", "1/1/2020", "2/1/2020", 
    "3/1/2020", "4/1/2020"), Value = c(3.5, 2.7, 4, 0.2, 2.5, 3.7, 
     0, 0.7, 3, 0, 1, 0.9), Group = c("A", "A", "A", "A", "B", "B", 
    "B", "B", "C", "C", "C", "C"), id = c(1L, 1L, 1L, 1L, 101L, 101L, 
    101L, 101L, 100L, 100L, 100L, 100L)), class = "data.frame", row.names = c(NA, 
   -12L))

Data is dummy and I have milions of records in original data set. 

Thanks for any help.
Elahe 
 



More information about the R-help mailing list