I have a long data set I would like to make wide and I'm curious if there is a way to do this all in one step using the reshape2 or tidyr packages in R.
The data frame df
looks like this:
id type transactions amount
20 income 20 100
20 expense 25 95
30 income 50 300
30 expense 45 250
I'd like to get to this:
id income_transactions expense_transactions income_amount expense_amount
20 20 25 100 95
30 50 45 300 250
I know I can get part of the way there with reshape2 via for example:
dcast(df, id ~ type, value.var="transactions")
But is there a way to reshape the entire df in one shot addressing both the "transactions" and "amount" variables at once? And ideally with new more appropriate column names?