Reputation: 4501
I have a dataframe which had the following structure:
Date Year quantity
19-JUN-15 2022 958
19-JUN-15 2021 894
18-JUN-15 2020 80
18-JUN-15 2019 96
and so on.
I want to transform this to look like this :
Date 2022 2021 2020 2019
19-JUN-15 958 894
18-JUN-15 80 96
and so on. Basically I want to make column 1 giving the dates and the rest of the columns are the individual years and the quantity matched according to the coordinates. How do I achieve this?
Upvotes: 0
Views: 62
Reputation: 887901
You can try
library(reshape2)
dcast(df1, Date~Year, value.var='quantity')
Or
library(tidyr)
spread(df1, Year, quantity)
Or
xtabs(quantity~Date+Year, df1)
Upvotes: 5