Reshaping data frame with duplicates

后端 未结 4 2141
庸人自扰
庸人自扰 2020-12-01 15:19

I have what should be a simple reshaping problem, but I can\'t figure it out. Part of my data looks like this:

foo <- structure(list(grade = c(3, 3, 4, 4,         


        
4条回答
  •  心在旅途
    2020-12-01 15:50

    library(plyr)
    library(reshape2)
    # First we add a grouping variable to deal with the duplicates
    foo <- ddply(foo, .(grade, var.type), function(x) { x$group <- 1:nrow(x); x })
    dcast(foo, grade + group ~ var.type, value.var= "var.val")[-2]
    
     grade SE  SS
    1      3 47 120
    2      3 12 120
    3      4 46 120
    4      4 14 120
    5      5 46 120
    6      5 16 120
    7      6 47 120
    8      6 20 120
    9      7 46 120
    10     8 46 120
    

提交回复
热议问题