I am trying to go from a dataframe with the following structure:
Date State Price.Name Cost.Name Price Cost
Jan AZ firm1.price firm1.cost 100 50
Jan AZ firm2.price firm2.cost 200 100
to a dataframe that looks like:
Date State firm1.price firm2.price firm1.cost firm2.cost
Jan AZ 100 200 50 100
I have tried using spread from tidyr:
data=spread(data,Price.Name, Price)
data=spread(data,Cost.Name, Cost)
But this gives me the following dataframe:
Date State firm1.price firm2.price firm1.cost firm2.cost
Jan AZ 100 NA 50 NA
Jan AZ NA 200 NA 100
I have found another thread addressing the same issue here: tidyr spread function generates sparse matrix when compact vector expected, but I couldn't determine if a solution was reached. Someone suggested grouping by a dummy variable before using spread
, but I have tried this to no avail.