0

我每个人都删除了我的上一篇文章,以对我的问题进行可重现的示例。我正在使用数据帧a1(dput 结构)的下一个:

structure(list(r04_numero_operacion = c("0050475725", "0050490602", 
"0050491033", "0050496386", "0050518985", "0050630090", "0050631615", 
"0060235906", "0060238732", "0060241333", "0060244391", "0060245813", 
"0060260056", "0060266356", "0800041441", "0800054041", "0800055382", 
"0800058554", "2020200062", "2020200073", "CAR1010001706000", 
"CAR1010001795000", "CAR1010001803000", "CAR1010001871000", "CAR1010001962000", 
"CAR1010002002000", "CAR1010002120000", "CAR1010002189000", "CAR1010002215000", 
"CAR1010002250000"), perdida3 = c(523.12, 265.43, 8371.66, 5242.13, 
4960.51, 8473.27, 3743.45, 1283.32, 2229.25, 8001.27, 8653.94, 
3670.13, 4536.02, 8216.55, 2481.36, 288.94, 1637.28, 4566.89, 
1573.63, 11217.92, 0, 0, 0, 0, 0, 0, 0, 0, 9633.9, 0), Saldo = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 288.94, 1637.28, 4566.89, 
1, 1, 481.59, 299.52, 258.13, 603.84, 231.61, 631.68, 220.6, 
210.54, 1, 1224.44), Bvencida = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 603.84, 0, 631.68, 
0, 0, 0, 0), Cvencida = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1224.44), 
    Dvencida = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), vencida = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 288.94, 1637.28, 
    4566.89, 1, 1, 0, 0, 0, 603.84, 0, 631.68, 0, 0, 1, 1224.44
    ), V1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("r04_numero_operacion", 
"perdida3", "Saldo", "Bvencida", "Cvencida", "Dvencida", "vencida", 
"V1"), codepage = 1252L, row.names = c(NA, 30L), class = "data.frame")

a2数据框(dput结构):

structure(list(r04_numero_operacion = c("0050475725", "0050490602", 
"0050491033", "0050496386", "0050518985", "0050630090", "0050631615", 
"0060235906", "0060238732", "0060241333", "0060244391", "0060245813", 
"0060260056", "0060266356", "0800041441", "0800054041", "0800055382", 
"0800058554", "2020200073", "CAR1010002002000", "CAR1010002189000", 
"CAR1010002215000", "CAR1010002250000", "CAR1010002264000", "CAR1010002297000", 
"CAR1010002401000", "CAR1010002412000", "CAR1010002436000", "CAR1010002529000", 
"CAR1010002709000"), perdida3 = c(523.12, 265.43, 8371.66, 5242.13, 
4960.51, 8473.27, 3743.45, 1283.32, 2229.25, 8001.27, 8653.94, 
3670.13, 4536.02, 8216.55, 2481.36, 288.94, 1637.28, 4566.89, 
11217.92, 0, 0, 9633.9, 0, 0, 0, 0, 0, 0, 0, 0), Saldo = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 288.94, 1637.28, 4566.89, 
1, 317.72, 210.54, 1, 868.93, 242.91, 298.78, 120.63, 255.01, 
357.68, 284.08, 308.83), Bvencida = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 317.72, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), Cvencida = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 868.93, 0, 0, 0, 0, 0, 0, 0), Dvencida = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), vencida = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 288.94, 1637.28, 4566.89, 1, 317.72, 0, 
1, 868.93, 0, 0, 0, 0, 0, 0, 0), V2 = c(2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2)), .Names = c("r04_numero_operacion", "perdida3", "Saldo", 
"Bvencida", "Cvencida", "Dvencida", "vencida", "V2"), class = "data.frame", row.names = c(NA, 
30L))

我的问题是当我使用merge()match()功能时。merge()match()通过公共变量添加新变量更实用,但是当我使用时,我merge()得到的结果与match(). 首先,我使用merge()a2创建a1DF一个代码:

DF=merge(a2,a1,all.x=TRUE)

它添加了V1变量 from a1to DF,我得到了以下摘要DF$V1

Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
  1       1       1       1       1       1       9 

在我创建了a2命名的副本并使用此代码将变量添加到以下位置DF进行匹配后:r04_numero_operacionV1a1a2

a2$V1<-a1[match(a2$r04_numero_operacion,a1$r04_numero_operacion),"V1"]

它添加`V1DF但结果与merge()方式不同。我得到了DF$V1解决方案的摘要match()

Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
  1       1       1       1       1       1       7 

我的问题是我想和我做的一样,match()但是由于这个函数而使用merge()函数比match(). 谢谢你的帮助。

4

1 回答 1

0

在使用match(a2$r04_numero_operacion,a1$r04_numero_operacion)a2$r04_numero_operacion 值时,与 a1 中的对应列匹配,而在使用merge(a2,a1,all.x=TRUE)a1 时,所有匹配列都与 a2 中的匹配列名匹配。如果您只匹配第一列,则 NA 计数匹配:

summary( merge(a2,a1,by=1,all.x=TRUE)$V1 )
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
      1       1       1       1       1       1       7 
于 2013-10-31T21:14:07.730 回答