can anyone help me figure out the code for transforming the following data in long format (Figure 1) to wide format (Figure 2)? I would like to use the wide format to make a heatmap. Thank you so much.
I've tried
dat_wide <- dcast(dat1,
id1+id2 ~ grade + year + category,
value.var = c("%level1", "%level2"))
Then an error message showed up saying that:
! Can't extract column with value.var
.
✖ Subscript value.var
must be size 1, not 2.
The main issue here would be calling the
dcast
function i.e.dcast
is available indata.table
andreshape2
packages. If the data isdata.table
, it calls thedata.table::dcast
instead of thereshape2::dcast
, but if it is a data.frame, it may callreshape2::dcast
(assuming both are loaded). Withdata.table::dcast
,whereas in
?reshape2::dcast
-testing
whereas
data