Reshape Data Long to Wide - understanding reshape parameters

前端 未结 3 1178
遥遥无期
遥遥无期 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:03

    In this case, using base reshape, you essentially want an interaction() of the three time variables to define your wide variables, so:

    idvars  <- c("dogid","home","school")
    grpvars <- c("year","month","trainingtype")
    outvar  <- "timeincomp"
    time    <- interaction(dat[grpvars])
    
    reshape(
      cbind(dat[c(idvars,outvar)],time),
      idvar=idvars,
      timevar="time",
      direction="wide"
    )
    
    #  dogid home school timeincomp.2014.1.1 timeincomp.2014.2.1 timeincomp.2015.12.2
    #1 12345    1      1                 340                 360                   NA
    #3 31323    7      3                 500                 520                  440
    

提交回复
热议问题