I have a wide-format data.table
like so:
library(data.table)
dt_wide <- data.table(
"id" = seq(1:10),
"yw_1001" = trunc( runif(10,0,100) ),
"yw_1002" = trunc( runif(10,0,100) ),
"yw_1003" = trunc( runif(10,0,100) ),
"yw_1004" = trunc( runif(10,0,100) ),
"yw_1005" = trunc( runif(10,0,100) ),
"yw_1006" = trunc( runif(10,0,100) ),
"yw_1007" = trunc( runif(10,0,100) ),
"yw_1008" = trunc( runif(10,0,100) ),
"yw_1009" = trunc( runif(10,0,100) ),
"yw_1010" = trunc( runif(10,0,100) ),
"yw_1011" = trunc( runif(10,0,100) ),
"yw_1012" = trunc( runif(10,0,100) ),
"yw_1013" = trunc( runif(10,0,100) ),
"yw_1014" = trunc( runif(10,0,100) ),
"yw_1015" = trunc( runif(10,0,100) ),
"yw_1016" = trunc( runif(10,0,100) ),
"yw_1017" = trunc( runif(10,0,100) ),
"yw_1018" = trunc( runif(10,0,100) ),
"yw_1019" = trunc( runif(10,0,100) ),
"yw_1020" = trunc( runif(10,0,100) ),
"yw_1021" = trunc( runif(10,0,100) ),
"yw_1022" = trunc( runif(10,0,100) ),
"yw_1023" = trunc( runif(10,0,100) ),
"yw_1024" = trunc( runif(10,0,100) ),
"yw_1025" = trunc( runif(10,0,100) ),
"yw_1026" = trunc( runif(10,0,100) ),
"yw_1027" = trunc( runif(10,0,100) ),
"yw_1028" = trunc( runif(10,0,100) ),
"yw_1029" = trunc( runif(10,0,100) ),
"yw_1030" = trunc( runif(10,0,100) ),
"yw_1031" = trunc( runif(10,0,100) ),
"yw_1032" = trunc( runif(10,0,100) ),
"yw_1033" = trunc( runif(10,0,100) ),
"yw_1034" = trunc( runif(10,0,100) ),
"yw_1035" = trunc( runif(10,0,100) ),
"yw_1036" = trunc( runif(10,0,100) ),
"yw_1037" = trunc( runif(10,0,100) ),
"yw_1038" = trunc( runif(10,0,100) ),
"yw_1039" = trunc( runif(10,0,100) ),
"yw_1040" = trunc( runif(10,0,100) ),
"yw_1041" = trunc( runif(10,0,100) ),
"yw_1042" = trunc( runif(10,0,100) ),
"yw_1043" = trunc( runif(10,0,100) ),
"yw_1044" = trunc( runif(10,0,100) ),
"yw_1045" = trunc( runif(10,0,100) ),
"yw_1046" = trunc( runif(10,0,100) ),
"yw_1047" = trunc( runif(10,0,100) ),
"yw_1048" = trunc( runif(10,0,100) ),
"yw_1049" = trunc( runif(10,0,100) ),
"yw_1050" = trunc( runif(10,0,100) ),
"yw_1051" = trunc( runif(10,0,100) ),
"yw_1052" = trunc( runif(10,0,100) )
)
The cols correspond to year (first two digits) and week number (last to digits).
In my actual dataset (nrow=5,500,000, ncol=1400), I cannot data.table::melt
because it would create a data.table over the row limit.
In reality, I only need values from certain week numbers. Take the following data.table
dt2 <- data.table(
"id" = seq(1:10),
"date" = sample(seq(as.Date('2010/01/01'), as.Date('2010/12/31'), by="day"), 10)
)
I need to keep the values in dt_wide that are 5, 10, and 15 weeks after the date in in dt2
for each unique id
. Ideally, want to reduce col size in dt_wide so I can melt to long format.
Any suggestions?
set.seed
while generating random data. Also show expected output for the data shared. It is not clear to me howdt2
is related todt_wide
. Also how column nameyw_1001
corresponds to year and week number. – Ronak Shah