【问题标题】:Merging rows within a data frame and merging columns between data frames合并数据框中的行和合并数据框之间的列
【发布时间】:2016-07-14 00:07:40
【问题描述】:

我一直在努力首先合并两个特定数据框之间的列,以及合并数据框中的行并将它们的值相加。我想首先将表 1 中的列“X”和“Y”添加到表 2 的末尾。在表 2 中,重复了一些“城镇”,例如“城镇 A”。我想合并这些行,同时将行中的数据相加。

表 1

       |        X|         Y |     
  |Town|
  |A   |       21|         23|           
  |A   |       21|         23|               
  |B   |       21|         23|               
  |C   |       21|         23|                
  |D   |       21|         23|                
  |D   |       21|         23|                
  |E   |       21|         23|                 
  |E   |       21|         23|               
  |F   |       21|         23|                
  |F   |       21|         23|                    

表 2

       |Species A| Species B | Species C| Species D| Species E | Species F |    
  |Town|
  |A   |       21|         23|        15|        0 |         3 |          7| 
  |A   |       21|         23|        15|        0 |         3 |          7| 
  |B   |       21|         23|        15|        0 |         3 |          7| 
  |C   |       21|         23|        15|        0 |         3 |          7| 
  |D   |       21|         23|        15|        0 |         3 |          7| 
  |D   |       21|         23|        15|        0 |         3 |          7| 
  |E   |       21|         23|        15|        0 |         3 |          7| 
  |E   |       21|         23|        15|        0 |         3 |          7| 
  |F   |       21|         23|        15|        0 |         3 |          7| 
  |F   |       21|         23|        15|        0 |         3 |          7|     

我尝试使用的一些代码是 c.bind 和 merge 函数,我也尝试使用 run.seq,如下所示:

run.seq <- function(x) as.numeric(ave(paste(x), x, FUN = seq_along))
L <- list(df1, df2) 
L2 <- lapply(L, function(x) cbind(x, run.seq = run.seq("Town"))) 
out <- Reduce(function(...) merge(..., all = TRUE), L2)[-2]

这不太奏效。

什么代码最适合这种类型的合并/组合?

如果有帮助,我将附上我的表格结构:

表 1

structure(list(Town = c("A", "A", "B", "C", "D", "D", "E", "E", "F", "F"), Captured = c(168L, 16L, 243L, 12L, 17L, 15L, 7L, 233L, 14L, 12L), Proportion = c(0.23, 0.02, 0.33, 0.02, 0.02, 0.02, 0.01, 0.32, 0.02, 0.02)), class = "data.frame", .Names = c("Town", "Captured", "Proportion"), row.names = c(NA,-10L))

表 2

structure(c(106L, 7L, 5L, 4L, 4L, 4L, 4L, 18L, 5L, 3L, 38L, 6L, 234L, 6L, 8L, 5L, 3L, 203L, 4L, 7L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 0L, 0L, 1L, 0L, 2L, 0L, 0L, 20L, 2L, 3L, 2L, 5L, 5L, 0L, 7L, 5L, 2L), .Dim = c(10L, 6L), .Dimnames = structure(list(Town = c("A", "A", "B", "C", "D", "D", "E", "E", "F", "F"), Species = c("funestus", "gambiae", "indeterminada", "outro", "pharoenois", "tenebrosus")), .Names = c("Town", "Species")), class = "table")

【问题讨论】:

  • 当我使用 cbind 时,说类似&lt;- cbind(ID, vp["Village"]) 的行数增加到 60 行,表中的所有数据都变得混乱。我尝试过的一些 cbind 和聚合变体发生了这种情况
  • 或类似merge(x=df1, y=df2, by = "Town", all.x = TRUE) 的事情也会发生。
  • 同样的事情发生了,物种,而不是列标题被列在一个新列中。行数增加到60,表中信息混杂。
  • 让您的示例可重现,以便我们可以使用它...发布 dput 而不是 str
  • 一些关于如何提供reproducible example 的信息。这将使其他人更容易帮助您。

标签: r merge


【解决方案1】:

最好先总结一下,然后合并/连接两个数据集。使用 table 2 的表格格式,您还可以使用 reshape2meltdcast 函数以及 sum 作为聚合函数(这会产生一个数据框) 然后与聚合的t1 数据框合并:

library(reshape2)
# aggragate 't1'
t1sum <- aggregate(.~Town, t1, sum)
# reshape and aggregate 't2'
t2sum <- dcast(melt(t2), Town ~ Species, fun.aggregate = sum)
# or with 'as.data.frame(t2)' instead of 'melt(t2)'
t2sum <- dcast(as.data.frame(t2), Town ~ Species, fun.aggregate = sum)

merge(t1sum, t2sum, by = 'Town')

给予:

  Town Captured Proportion funestus gambiae indeterminada outro pharoenois tenebrosus
1    A      184       0.25      113      44             1     2          2         22
2    B      243       0.33        5     234             0     0          1          3
3    C       12       0.02        4       6             0     0          0          2
4    D       32       0.04        8      13             0     0          1         10
5    E      240       0.33       22     206             0     0          2          7
6    F       26       0.04        8      11             0     0          0          7

使用 data.table 包你可以做类似的操作:

library(data.table)
t1dt <- setDT(t1)[, lapply(.SD, sum), by = Town]
t2dt <- dcast(setDT(melt(t2)), Town ~ Species, fun.aggregate = sum)

t1dt[t2dt, on='Town']

使用过的数据:

t1 <- structure(list(Town = c("A", "A", "B", "C", "D", "D", "E", "E", "F", "F"), Captured = c(168L, 16L, 243L, 12L, 17L, 15L, 7L, 233L, 14L, 12L), Proportion = c(0.23, 0.02, 0.33, 0.02, 0.02, 0.02, 0.01, 0.32, 0.02, 0.02)), class = "data.frame", .Names = c("Town", "Captured", "Proportion"), row.names = c(NA,-10L))
t2 <- structure(c(106L, 7L, 5L, 4L, 4L, 4L, 4L, 18L, 5L, 3L, 38L, 6L, 234L, 6L, 8L, 5L, 3L, 203L, 4L, 7L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 0L, 0L, 1L, 0L, 2L, 0L, 0L, 20L, 2L, 3L, 2L, 5L, 5L, 0L, 7L, 5L, 2L), .Dim = c(10L, 6L), .Dimnames = structure(list(Town = c("A", "A", "B", "C", "D", "D", "E", "E", "F", "F"), Species = c("funestus", "gambiae", "indeterminada", "outro", "pharoenois", "tenebrosus")), .Names = c("Town", "Species")), class = "table")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多