I am trying to replace NA with a random sample from the corresponding group. For example, in line 2 of the National Assembly from "France" with an age and time of "20-30" 30-40. Therefore, I want to take a random sample of the column "Answer" for all other observations "France", "20-30", "30-40".
I have the code below that works fine, but each value is replaced with the same random sample. For example, if I had more than one βFranceβ, β20-30β, β30-40β, then both of their respective R2 would be the same.
I would like each NA to be selected independently, but data.table seems to do it all at once, so I cannot do this. Any ideas?
DT <- data.table(mydf, key = "Country,Age,Time")
DT[, R2 := ifelse(is.na(Response), sample(na.omit(Response), 1),
Response), by = key(DT)]
DT
where is mydf
mydf <- structure(list(Index = 1:7, Country = c("Germany", "Germany",
"Germany", "Germany", "France", "France", "France"), Age = c("20-30",
"20-30", "20-30", "20-30", "20-30", "20-30", "20-30"), Time = c("15-20",
"15-20", "15-20", "15-20", "30-40", "30-40", "30-40"), Response = c(1L,
NA, 1L, 0L, 1L, NA, 2L)), .Names = c("Index", "Country", "Age",
"Time", "Response"), class = "data.frame", row.names = c(NA, -7L))