开发者

Why can't I add numbers in a dataframe created with cbind?

开发者 https://www.devze.com 2023-04-01 16:06 出处:网络
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开发者_StackOverf

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开发者_StackOverflow社区 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]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号