Reshaping data frame with duplicates

后端 未结 4 2133
庸人自扰
庸人自扰 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:39

    It is not as pretty as reshape, but

    data.frame(grade = foo[2 * (1:(nrow(foo)/2)),]$grade, 
               SS =  foo[foo$var.type == "SS", ]$var.val, 
               SE =  foo[foo$var.type == "SE", ]$var.val ) 
    

    produces

       grade  SS SE
    1      3 120 47
    2      4 120 46
    3      5 120 46
    4      6 120 47
    5      7 120 46
    6      8 120 46
    7      3 120 12
    8      4 120 14
    9      5 120 16
    10     6 120 20
    

    You have to assume the data comes in pairs of rows for this.

提交回复
热议问题