【问题标题】:Reshaping data frame with duplicates用重复数据重塑数据框
【发布时间】:2012-10-01 13:16:22
【问题描述】:

我有一个简单的重塑问题,但我想不通。我的部分数据如下所示:

foo <- structure(list(grade = c(3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 
3, 3, 4, 4, 5, 5, 6, 6), var.type = structure(c(3L, 2L, 3L, 2L, 
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L
), .Label = c("Raw Score", "SE", "SS"), class = "factor"), var.val = c(120L, 
47L, 120L, 46L, 120L, 46L, 120L, 47L, 120L, 46L, 120L, 46L, 120L, 
12L, 120L, 14L, 120L, 16L, 120L, 20L)), .Names = c("grade", "var.type", 
"var.val"), row.names = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L), class = "data.frame")

  grade var.type var.val
2     3       SS     120
3     3       SE      47
4     4       SS     120
5     4       SE      46
6     5       SS     120
7     5       SE      46

我想让它看起来像这样:

grade    SS    SE
3        120   47
4        120   46
5        120   46

等等。我已经尝试过 reshape、cast 和 dcast,就像在这个线程中一样:

Reshaping dataset

但似乎没有任何效果。我真的很感激一些帮助。 TIA。

【问题讨论】:

  • 在您上面提供给我们的示例数据中,有一行 (grade, var.type, var.val) = (3, SE, 47),还有一个与 (3, SE, 12)。您希望哪个var.val 出现在输出SE 列中?
  • 哦,对不起。我搞砸了。看来我需要添加另一个变量。坚果。不过比我复杂。
  • 只需用新数据更新问题。

标签: r reshape2


【解决方案1】:

如果你想重塑并且你有重复,你需要给每一对一个唯一的 id:

foorle <- rle(foo$grade)
fooids <- rep(seq_len(length(foorle$values)), times=foorle$lengths)

fooids
 [1]  1  1  2  2  3  3  4  4  5  5  6  6  7  7  8  8  9  9 10 10

现在您可以正确使用 reshape 了:

idfoo <- cbind(id=fooids, foo)

library(reshape)
dcast(idfoo, id+grade~var.type, value.var="var.val")

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

编辑:请注意,我假设您的数据是有序的,否则您将无法区分重复项。如果不是,您可以随时使用order,这样就可以了。

【讨论】:

  • 我也向类似的问题推荐this answer,我认为它比我的方法更好。
【解决方案2】:

如果您没有有任何重复,这将很好地工作:

ss <- subset(foo, var.type=='SS')
se <- subset(foo, var.type=='SE')
ss <- data.frame(grade=ss$grade,SS=ss$var.val)
se <- data.frame(grade=se$grade,SE=se$var.val)
bar <- merge(ss,se,by='grade')

【讨论】:

    【解决方案3】:
    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
    

    【讨论】:

      【解决方案4】:

      没有reshape好看,但是

      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 ) 
      

      生产

         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
      

      为此,您必须假设数据成对出现。

      【讨论】:

        猜你喜欢
        • 2019-05-29
        • 2014-01-31
        • 2014-11-09
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多