1

仍然了解 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 坐标,然后在两个数据帧之间进行比较。任何帮助将不胜感激!

4

2 回答 2

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
于 2013-07-05T20:21:55.903 回答
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
于 2013-07-05T20:45:23.047 回答