在两个数据帧中组合相似的行

时间:2013-07-05 19:48:51

标签: r

仍然得到了R的要点。我有两个数据框,其中行以不同的坐标命名(例如x_1013y_41403;见下文)。坐标形成五个集合,每个集合在绘制到网格上时形成十字形。中心坐标位于一个数据框中,四个外围坐标位于另一个数据框中。

Center                  A       B       C      D       E       F
x_723y_6363.txt       554      NA     604     NA     645      NA
x_749y_41403.txt       14      NA       6     NA      13      NA

Peripheral              A       B       C      D       E       F
x_1013y_41403.txt      NA       1      NA      0      NA       0
x_459y_6363.txt        NA       2      NA      1      NA       4
x_485y_41403.txt       NA       0      NA      0      NA       0
x_723y_6100.txt        NA       1      NA      0      NA       3
x_723y_6627.txt        NA       1      NA      0      NA       1
x_749y_41139.txt       NA       1      NA      0      NA       0
x_749y_41667.txt       NA       2      NA      0      NA       0
x_987y_6363.txt        NA       1      NA      0      NA       0

要形成一组,外围坐标将具有与中心坐标相同的x或y位置。例如,中心坐标x_723y_6363将与x_723y_6100和x_723y_6627(相同的x位置)以及x_459y_6363和x_987y_6363(相同的y位置)相关联。

我想将坐标组合到各自的集合中,并使用中心坐标命名集合。对于上面的情况,我最终会得到两行,其中每一行都是一个集合的总和。

                        A       B       C      D       E       F
x_723y_6363.txt       554       5     604      1     645       8
x_749y_41403.txt       14       4       6      0      13       0

我根本不确定如何做到这一点。我已经考虑过创建正则表达式来单独选择x和y坐标,然后在两个数据帧之间进行比较。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

我希望其他人能提出更好的答案,因为这很丑陋。我首先将.txt名称拆分为x和y值,然后循环遍历中心为NA的每个变量,并将与该中心共享x或y值的所有值相加。 修改:更改了sapply以使其更加出色。

center <- read.table(textConnection("                                                                                                                                                          
A B C D E F                                                                                                                                                                                    
x_723y_6363.txt       554      NA     604     NA     645      NA                                                                                                                               
x_749y_41403.txt       14      NA       6     NA      13      NA"),
                     header = TRUE)

peripheral <- read.table(textConnection("                                                                                                                                                      
A       B       C      D       E       F                                                                                                                                                       
x_1013y_41403.txt      NA       1      NA      0      NA       0                                                                                                                               
x_459y_6363.txt        NA       2      NA      1      NA       4                                                                                                                               
x_485y_41403.txt       NA       0      NA      0      NA       0                                                                                                                               
x_723y_6100.txt        NA       1      NA      0      NA       3                                                                                                                               
x_723y_6627.txt        NA       1      NA      0      NA       1                                                                                                                               
x_749y_41139.txt       NA       1      NA      0      NA       0                                                                                                                               
x_749y_41667.txt       NA       2      NA      0      NA       0                                                                                                                               
x_987y_6363.txt        NA       1      NA      0      NA       0"),
                         header = TRUE)

xpat <- "^([^y]+).*"
ypat <- ".*(y_[0-9]+)\\.txt"
center$x <- gsub(xpat, "\\1", rownames(center))
center$y <- gsub(ypat, "\\1", rownames(center))
peripheral$x <- gsub(xpat, "\\1", rownames(peripheral))
peripheral$y <- gsub(ypat, "\\1", rownames(peripheral))


vars <- c("B", "D", "F")

center[vars] <- sapply(peripheral[vars], function(col)
  apply(center, 1, function(row) sum(col[peripheral$x %in% row["x"] | peripheral$y %in% row["y"]]) )
  )

R> center
                    A B   C D   E F     x       y
 x_723y_6363.txt  554 5 604 1 645 8 x_723  y_6363
 x_749y_41403.txt  14 4   6 0  13 0 x_749 y_41403

答案 1 :(得分:1)

另一种选择:

# function to split coordinates x and y:

f <- function(DF) structure(
    t(sapply(strsplit(row.names(DF), "[_y.]"), `[`, c(2,4))),
    dimnames=list(NULL, c("x", "y")))

# get x and y for peripheral data:

P <- cbind(Peripheral, f(Peripheral))

# get x and y for centers, and mark ids:

C <- cbind(Center, f(Center), id=1:nrow(Center))

# matching:

Q <- merge(merge(P, C[,c("x","id")], all=TRUE), C[,c("y","id")], by="y", all=TRUE)

# prepare for union:

R <- within(Q, {id <- ifelse(is.na(id.y), id.x, id.y); id.x <- NULL; id.y <- NULL})

# join everything and aggregate:

S <- rbind(R, C)

aggregate(S[,3:8], by=list(id=S$id), FUN=sum, na.rm=TRUE)

结果:

  id   A B   C D   E F
1  1 554 5 604 1 645 8
2  2  14 4   6 0  13 0
相关问题