While working with R, reshaping dataframe from wide format to long format is relatively easier than the opposite. Especially when you want to reshape a dataframe to a wide format with multiple columns for value.var in dcast. Let’s look at an example dataset:
From v1.9.6 of data.table, we can cast multiple value.var by this syntax:
testWide <- dcast(setDT(test), formula = time ~ country, value.var = c(‘feature1′,’feature2’))
All you need is add ‘setDT’ for the dataframe and pass the list of value.var to it.