【问题标题】:How to merge different rows of different tables into a new one如何将不同表的不同行合并成一个新表
【发布时间】:2016-05-02 15:41:13
【问题描述】:

我得到了data.frame (df). 的这两个表(A 和 B)

表 A

             bb  cc    dd    W   ee   Y
vaccined     12  17.2  15.3  14  5.1  9
no_vaccined  13  7.8   9.7   11  24.9 16
NA           0.3 0.6   1.3   0   0.5  0.7

表 B

    bb   cc   dd ee  
no  91.5 90.8 95 87
yes 8.5  9.2  5  13

注:在表 B 中,“是”代表以百分比表示的受试者数量(例如“请注意,表 B 没有“W”或“Y”变量

我想将这两个表合并为一个。但是:我只想在新表 (C) 中添加表 B 的“是”行并保留表 A 的“W”和“Y”变量。

例子:

             bb   cc    dd    W  ee    Y
vaccined     12   17.2  15.3  14  5.1  9
no_vaccined  13   7.8   9.7   11  24.9 16
NA           0.3  0.6   1.3   0   0.5  0.7
yes          8.5  9.2   5     25  13   25.7

有没有办法做到这一点?

任何帮助都会非常有帮助。

【问题讨论】:

  • 我可以帮助你,但你必须给我一些可重复的数据。查看dput() 函数。
  • 总的来说,我认为您正在寻找的是 plyr 包中的 rbind.fill 。试试 rbind.fill(table_a,table_b)。但@Raphael K 是对的,您需要一个可重现的示例让我们确切知道您想要什么。
  • 合并后的data.frame中yes行的WY值是从哪里来的?其他行的总和?

标签: r


【解决方案1】:

如果表 B 有 NoYes 作为行名,您可能可以执行以下操作

rows.to.keep<-c("Yes")# you can add more.  
dfC<-which(rownames(dfB) %in% rows.to.keep)
new.df <- rbind(dfA,dfC) 

【讨论】:

    【解决方案2】:

    你可以试试这个:

    # sum up W and Y
    # and add sum(Y) and sum(W) to B in the same column-order as in table A:
    B_new<- cbind(B[, 1:3], W=sum(A$W),ee= B[, 4], Y=sum(A$Y))
    
    rbind(A, B_new[2, ])
                  bb   cc   dd  W   ee    Y
    vaccined    12.0 17.2 15.3 14  5.1  9.0
    no_vaccined 13.0  7.8  9.7 11 24.9 16.0
    NA           0.3  0.6  1.3  0  0.5  0.7
    yes          8.5  9.2  5.0 25 13.0 25.7
    

    首先,通过使用cbind 添加YW 两列,将表B 调整为类似于表A。正如您在预期输出中显示的那样,分别对这些值求和。为此,您可以使用 sum 函数。然后你可以rbind A 和 B(只有“是”行)在一起。

    【讨论】:

      【解决方案3】:

      如果您在两个 data.frame 中没有相同的变量,使用 merge 比使用 rbind 更安全:

      AB <- merge(A, B[B[, 1] == 'yes', ], all = TRUE)
      AB
      #           var   bb   cc   dd   ee  W    Y
      # 1 no_vaccined 13.0  7.8  9.7 24.9 11 16.0
      # 2    vaccined 12.0 17.2 15.3  5.1 14  9.0
      # 3         yes  8.5  9.2  5.0 13.0 NA   NA
      # 4        <NA>  0.3  0.6  1.3  0.5  0  0.7
      

      您似乎还想通过插入列总和来估算NAs 中WY 的值,您可以这样做:

      AB[, -1][is.na(AB[, -1])] <- colSums(AB[, -1][,colSums(is.na(AB[, -1])) > 0], na.rm = TRUE)
      AB
      #           var   bb   cc   dd   ee  W    Y
      # 1 no_vaccined 13.0  7.8  9.7 24.9 11 16.0
      # 2    vaccined 12.0 17.2 15.3  5.1 14  9.0
      # 3         yes  8.5  9.2  5.0 13.0 25 25.7
      # 4        <NA>  0.3  0.6  1.3  0.5  0  0.7
      

      如果我在这里读到的var 实际上是行名,您可以删除[, -1]s。您还需要将merge 中的[, 1] 替换为row.names(B)


      数据

      A <- structure(list(var = structure(c(2L, 1L, NA), .Label = c("no_vaccined", 
          "vaccined"), class = "factor"), bb = c(12, 13, 0.3), cc = c(17.2, 
          7.8, 0.6), dd = c(15.3, 9.7, 1.3), W = c(14L, 11L, 0L), ee = c(5.1, 
          24.9, 0.5), Y = c(9, 16, 0.7)), .Names = c("var", "bb", "cc", 
          "dd", "W", "ee", "Y"), class = "data.frame", row.names = c(NA, 
          -3L))
      
      B <- structure(list(var = structure(1:2, .Label = c("no", "yes"), class = "factor"), 
          bb = c(91.5, 8.5), cc = c(90.8, 9.2), dd = c(95L, 5L), ee = c(87L, 
          13L)), .Names = c("var", "bb", "cc", "dd", "ee"), class = "data.frame", row.names = c(NA, 
          -2L))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-10
        • 1970-01-01
        • 2018-07-05
        相关资源
        最近更新 更多