-3

I got a data like below:

1.Apr.15  1.Apr.16  1.Aug.14 
   549       655        3  

I am wondering how to change this data into a table like:

       14       15       16
1.Apr           549      655
1.Aug  3

I tried to use table but it did not give me the correct result.

  • 2
    Please provide a reproducible example *and* show us what you've tried. See [ask] and [mcve] for details on how to do this. Also, for R questions, [How to make a great R reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is useful. – Rich Scriven Nov 02 '16 at 19:13

1 Answers1

0

This may be close, if not exactly what you want, using the tidyr package. Note that I changed the formatting of your dates (to month.day.year), because column names in data frames cannot begin with numbers.

d <- data.frame("Apr.1.15" = 549, "Apr.1.16" = 655, "Aug.1.14" = 3)

library(tidyr)
gather(d) %>% 
    separate(key, c("month", "day", "year"), sep = "\\.") %>% 
    spread(year, value)

#   month day 14  15  16
# 1   Apr   1 NA 549 655
# 2   Aug   1  3  NA  NA
Daniel Anderson
  • 2,394
  • 13
  • 26