0

I have the following data frame.

sample_data <- data.frame(Date = c ("2017-01-31", "2017-01-31","2017-02-28", "2017-01-31", "2017-03-31","2017-02-28", "2017-03-31", "2017-04-30", "2017-02-28","2017-03-31","2017-04-30","2017-04-30"), 
                  stock = c("c", "a", "a", "b", "a", "b", "b", "a", "c", "c", "b", "c"), 
                  Price = c(9,6,7,8,10,12,13, 11, 17, 12, 13, 14))

I am looking to rearrange this so that the stock letter are assigned to a column instead of all being in the second column. So the data frame would only show four dates and 4 prices for every stock (stock "a" would be one column, "b" the next etc etc).

user8491385
  • 413
  • 1
  • 5
  • 17

1 Answers1

1

The tidyr package provides one way to do this.

library(tidyr)
sample_data %>% 
 spread(stock, Price)
markus
  • 25,843
  • 5
  • 39
  • 58