Reshape Data Long to Wide - understanding reshape parameters

前端 未结 3 1183
遥遥无期
遥遥无期 2020-12-04 00:44

I have a long format dataframe dogs that I\'m trying to reformat to wide using the reshape() function. It currently looks like so:

dogid  month  year  traini         


        
3条回答
  •  长情又很酷
    2020-12-04 01:15

    You can do the same thing using the new replacement for reshape2, tidyr:

    library(tidyr)
    library(dplyr)
    data %>% unite(newcol, c(year, month, trainingtype)) %>%
             spread(newcol, timeincomp)
    
      dogid home school 2014_1_1 2014_2_1 2015_12_2
    1 12345    1      1      340      360        NA
    2 31323    7      3      500      520       440
    

    First, we unite the year, month and trainingtype columns into a new column called newcol, then we spread the data with timeincomp as our value variable.

    The NA is there as we have no value, you can give it one by changing fill = NA in the spread function.

提交回复
热议问题