문제

I’m looking for advice on how to subtract values from each other listed in two data frames. In the example below with the two data frames A and B, I would like to subtract the values of the 2nd columns from each other on the condition that the first column vectors are matching. For example, when vector X1 is R1 then 5.1 - 5 and 4.8 - 5.

A<-data.frame(cbind(c('R1','R1','R2','R4','R4','R4'),c(5.1,4.8,4.9,5.0,5.0,5.3)))

B<-data.frame(cbind(c('R1','R2','R3','R4'),c(5,4.9,5.2,5.1)))

도움이 되었습니까?

해결책

General R advice: DON'T USE cbind WHEN IT'S NOT NECESSARY:

A <- data.frame(X1=c('R1','R1','R2','R4','R4','R4'),X2=c(5.1,4.8,4.9,5.0,5.0,5.3))
B <- data.frame(X1=c('R1','R2','R3','R4'),X2=c(5,4.9,5.2,5.1))

(You made factors of those numbers. And you cannot apply arithmetic operators to factors.)

When there are non-unique matches, the merge function returns all possible pairs:

merge(A,B, by=1)
merge(A,B, by=1)[,2] - merge(A,B, by=1)[,3]
#[1]  0.1 -0.2  0.0 -0.1 -0.1  0.2

다른 팁

Define your data so that the numeric columns really are numeric. (With spaces for readability!)

A <- data.frame(
  X1 = c('R1', 'R1', 'R2', 'R4', 'R4', 'R4'), 
  X2 = c(5.1, 4.8, 4.9, 5.0, 5.0, 5.3)
)
B <- data.frame(
   X1 = c('R1', 'R2', 'R3', 'R4'), 
   X2 = c(5, 4.9, 5.2, 5.1)
)

Merge the data frames, then subtract columns with the result.

merged <- merge(A, B, "X1")
with(merged, X2.x - X2.y)

Here is solution with vector names.

A <- data.frame(
  X1 = c('R1', 'R1', 'R2', 'R4', 'R4', 'R4'), 
  X2 = c(5.1, 4.8, 4.9, 5.0, 5.0, 5.3),
  stringsAsFactors = FALSE
)

L <- c(5, 4.9, 5.2, 5.1)
names(L) <- c('R1', 'R2', 'R3', 'R4')

A$X2-L[A$X1]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top