17

我有两个数据框:(这些是它们的缩短版本)

一个

    Link    VU  U   P
1   DVH1    7   1   37
2   DVH2    7   0   38
3   DVH3    10  1   35

    Link    VU  U   P
1   DVH1    2   0   15
2   DVH2    4   0   14
3   DVH3    0   0   5

我想根据它们的位置从 A 中的值中减去数据框 B 中的值。例如:对于 DVH1,VU 为 7-2(或 5),生成的数据帧如下所示:

    Link    VU  U   P
1   DVH1    5   1   22
2   DVH2    3   0   24
3   DVH3    10  1   30
4

2 回答 2

12

Use this:

within(merge(A,B,by="Link"), {
    VU <- VU.x - VU.y
    U <- U.x - U.y
    P <- P.x - P.y
})[,c("Link","VU","U","P")]

EDIT: Bonus: if there are too many paired columns (not just VU, U and P) you can use this:

M <- merge(A,B,by="Link")

S <- M[,grepl("*\\.x$",names(M))] - M[,grepl("*\\.y$",names(M))]

cbind(M[,1,drop=FALSE],S)

#  Link VU.x U.x P.x
#1 DVH1    5   1  22
#2 DVH2    3   0  24
#3 DVH3   10   1  30
于 2013-09-09T23:16:53.547 回答
11

merge(最有可能)更快的方法是确保第二个data.frame与第一个的行和列顺序相同,然后将它们彼此相减:

z <- names(A)[-1]
cbind(A[1], A[z] - B[match(A$Link, B$Link), z])
#   Link VU U  P
# 1 DVH1  5 1 22
# 2 DVH2  3 0 24
# 3 DVH3 10 1 30

以下是一些示例数据:

A <- structure(list(Link = c("DVH1", "DVH2", "DVH3"), VU = c(7L, 7L, 
10L), U = c(1L, 0L, 1L), P = c(37L, 38L, 35L)), .Names = c("Link", 
"VU", "U", "P"), class = "data.frame", row.names = c("1", "2", "3"))

B <- structure(list(Link = c("DVH1", "DVH3", "DVH2"), P = c(15L, 5L, 
14L), U = c(0L, 0L, 0L), VU = c(2L, 0L, 4L)), .Names = c("Link", 
"P", "U", "VU"), class = "data.frame", row.names = c("1", "3", "2"))
于 2013-09-10T01:55:53.177 回答