I have a data frame like the one generated below. The id column is not unique I also have a data frame of 365 columns, one for each day. The "day" column represents the day of the year. I would like each row of the second data frame to map to the id column with 1s for the days that are present with that id and 0s otherwise. The data are not ordered neatly like in the example data frame.
data <- data.frame(id = 1:100, day = as.integer(runif(100, 0, 364)))
The expected output for an id that had the values 0, 3, and 364 in the original data frame would be:
id day0 day1 day2 day3 ... day364
1 1 0 0 1 1
I am also open to solutions that involve using data.table
. Thank you!
reshape
ordcast
from thereshape2
package... but its very unclear what you want exactly. Can you make a reproducible example and include the whole desired output. – Justinas.integer(runif(n, 0, 364))
cannot result in364
. Usesample(0:364, n, replace=TRUE)
. – Ferdinand.kraftwith(data, table(id, day))
. – Thomas