2017-05-08 66 views

回答

1

随着tidyrzoo,这个只需要几个步骤。如果你也加载dplyr,你可以管它。假设你的表格被称为df ...

library(dplyr) 
library(tidyr) 
library(zoo) 

df %>% 
    # use tidyr to convert the wide data to long 
    gather(key = month, value = value, -Year) %>% 
    # use zoo to create a year-month index 
    mutate(yearmon = as.yearmon(paste(month, Year))) %>% 
    # now make a zoo object of the values with that index 
    with(., zoo(value, order.by = yearmon)) 
相关问题