从一个数据框中减去另一个数据框中的值

时间:2021-12-18 21:32:03

I have two data frames: (these are shortened versions of them)

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

A

一个

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

B

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

I want to substract the values in data frame B from those in A based on their location. So for example: For DVH1, VU would be 7-2 (or 5), and the resulting data frame would look like:

我想根据它们的位置从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

2 个解决方案

#1


8  

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:

编辑:奖励:如果配对列太多(不仅仅是VU,U和P),您可以使用:

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

#2


5  

A faster way than merge (most likely) is to just make sure the second data.frame is in the same row and column order as the first and subtract them from each other:

比合并更快的方法(最有可能)是确保第二个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

Here's some sample data:

这是一些示例数据:

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"))

#1


8  

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:

编辑:奖励:如果配对列太多(不仅仅是VU,U和P),您可以使用:

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

#2


5  

A faster way than merge (most likely) is to just make sure the second data.frame is in the same row and column order as the first and subtract them from each other:

比合并更快的方法(最有可能)是确保第二个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

Here's some sample data:

这是一些示例数据:

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"))