I wonder if there is a way to fill
with random numbers each individual missing value when using dcast
(library reshape2
or data.table
). Example:
ID = c('AA', 'AA', 'BB', 'BB', 'CC', 'CC', 'CC', 'DD', 'DD')
Replica = c('H1','H3','H1','H2','H1','H2','H3','H2','H3')
Value = c(1.3, 2.5, 1.4, 3.7, 9.5, 7.4, 7.1, 1.8, 8.4)
example <- data.frame(ID=ID, Replica = Replica, Value = Value)
Doing a simple dcast
dfdc <- dcast(data=example, ID~Replica, value.var = 'Value', fill = sample(1:10, 1))
notice how some of the values are missed:
ID H1 H2 H3
1 AA 1.3 NA 2.5
2 BB 1.4 3.7 NA
3 CC 9.5 7.4 7.1
4 DD NA 1.8 8.4
I would like to fill up each of those missing values with random numbers, something like:
dfdc <- dcast(data=example, ID~Replica, value.var = 'Value', fill = sample(1:10, 1))
which gives as a result:
ID H1 H2 H3
1 AA 1.3 2.0 2.5
2 BB 1.4 3.7 2.0
3 CC 9.5 7.4 7.1
4 DD 2.0 1.8 8.4
However, all the missing values have been replaced by the same random number (2 in this case).
Would it be possible to apply the function individually to each missing value and, therefore, fill the missing values with different random numbers?
Thanks in advance!