Reshaping: Long to Wide
dcast(){reshape2}
install.packages("reshape2")
library(reshape2)
Syntax of dcast()
id variables ~ variable variables
ideDataFrame <- dcast(longDataFrame, idVariableColumn1 + idVariableColumn2 ~ variableColumn, value.var="Nameolumn with the measurement values")
[S]pecify the formula where
- ID variables
- The variables you want repeating in each row
- those that remain in columns
- are before the tilde (~)
- variable variables
- Those that should become column headers
- those that get 'moved to the top'
- are after the tilde (~)
- The third argument for dcast() -assigns the name of the column that holds your measurement values to value.var.
- ID variables
Code of dcast()
companiesWide <- dcast(companiesLong, fy + company ~ financialCategory, value.var="amount")