[R] merging data frame

arnaud Gaboury arnaud.gaboury at gmail.com
Tue May 11 14:07:39 CEST 2010


Dear group,

I have 3 data frames I would like to merge.

Here they are:

pose16 <-
structure(list(DESCRIPTION = structure(c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 12L), .Label = c("COPPER May/10", "COTTON NO.2 Jul/10", 
"CRUDE OIL miNY May/10", "GOLD Jun/10", "ROBUSTA COFFEE (10) Jul/10", 
"SOYBEANS Jul/10", "SUGAR NO.11 Jul/10", "SUGAR NO.11 May/10", 
"WHEAT Jul/10", "PRIMARY NICKEL USD", "PRM HGH GD ALUMINIUM USD", 
"SPCL HIGH GRADE ZINC USD", "STANDARD LEAD USD"), class = "factor"), 
    POSITION = c(2, -3, -2, -2, -18, -3, -5, 1, -5, 1), prix = c(702.9, 
    -244.77, -166.48, -2273.8, -24876, -2985, -80.9, 15.95, -2512.5, 
    2423)), .Names = c("DESCRIPTION", "POSITION", "prix"), row.names =
c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "51"), class = "data.frame")

pose15 <-
structure(list(DESCRIPTION = structure(1:9, .Label = c("CORN May/10", 
"COTTON NO.2 Jul/10", "CRUDE OIL miNY May/10", "ROBUSTA COFFEE (10) Jul/10",

"SILVER May/10", "SOYBEANS Jul/10", "SUGAR NO.11 Jul/10", "SUGAR NO.11
May/10", 
"WHEAT May/10"), class = "factor"), POSITION = c(3, 4, 3, 10, 
1, 1, 3, -1, 6), prix = c(1089.75, 328.48, 256.53, 13860, 1843.3, 
993, 51.24, -16.85, 2881.5)), .Names = c("DESCRIPTION", "POSITION", 
"prix"), class = "data.frame", row.names = c(NA, 9L))

avprix16 <-
structure(list(DESCRIPTION = c("COPPER May/10", "CORN May/10", 
"COTTON NO.2 Jul/10", "CRUDE OIL miNY May/10", "GOLD Jun/10", 
"ROBUSTA COFFEE (10) Jul/10", "SILVER May/10", "SOYBEANS Jul/10", 
"SPCL HIGH GRADE ZINC USD", "SUGAR NO.11 Jul/10", "WHEAT Jul/10", 
"WHEAT May/10", "COFFEE C Jul/10", "CORN Jul/10", "HENRY HUB NATURAL GAS
May/10", 
"STANDARD LEAD USD"), prix = c(-716.9, -1082, -79.96, -84.825, 
2295.5, 11084, -1798, 1983.5, -2464, 32.39, 2448.25, -2881.5, 
4.75000000000003, -1.5, -0.0490000000000013, -118), POSITION = c(-2, 
-3, -1, -1, 2, 8, -1, 2, -1, 2, 5, -6, 0, 0, 0, 0), fees = c(-47.52, 
-30.24, -20.05, -107.59, -63.36, -26.4, -43.56, -25.92, -37.5, 
-96.24, -56.16, -25.92, -24.06, -8.64, -23.76, -25)), .Names =
c("DESCRIPTION", 
"prix", "POSITION", "fees"), row.names = c(2L, 4L, 5L, 6L, 7L, 
9L, 10L, 11L, 12L, 14L, 15L, 16L, 1L, 3L, 8L, 13L), class = "data.frame")

As you can see, they have three common columns : $prix, $DESCRIPTION and
$POSITION. I want to merge them by $DESCRIPTION, with
$prix=pose16$prix+pose15$prix+avprix16$prix,
$POSITION=pose16$POSITION+pose15$POSITION+avprix16$POSITION
I managed to do it, but with many command lines. Maybe is there a simple way
to do it?

TY for help.



More information about the R-help mailing list