[R] merging data frame

arnaud Gaboury arnaud.gaboury at gmail.com
Tue May 11 14:33:58 CEST 2010


In fact, that is one solution. I can then do a
ddply(x,"DESCRIPTION",summarise, pl=sum(prix),quantity=sum(POSITION)). It
does work. I was just wandering if it exists another solution.




From: jim holtman [mailto:jholtman at gmail.com] 
Sent: Tuesday, May 11, 2010 2:14 PM
To: arnaud Gaboury
Cc: r-help at r-project.org
Subject: Re: [R] merging data frame

If you just want to combine them together (rbind), try this:
 
> str(avprix16)
'data.frame':   16 obs. of  4 variables:
 $ DESCRIPTION: chr  "COPPER May/10" "CORN May/10" "COTTON NO.2 Jul/10"
"CRUDE OIL miNY May/10" ...
 $ prix       : num  -716.9 -1082 -80 -84.8 2295.5 ...
 $ POSITION   : num  -2 -3 -1 -1 2 8 -1 2 -1 2 ...
 $ fees       : num  -47.5 -30.2 -20.1 -107.6 -63.4 ...
> str(pose15)
'data.frame':   9 obs. of  3 variables:
 $ DESCRIPTION: Factor w/ 9 levels "CORN May/10",..: 1 2 3 4 5 6 7 8 9
 $ POSITION   : num  3 4 3 10 1 1 3 -1 6
 $ prix       : num  1090 328 257 13860 1843 ...
> str(pose16)
'data.frame':   10 obs. of  3 variables:
 $ DESCRIPTION: Factor w/ 13 levels "COPPER May/10",..: 1 2 3 4 5 6 7 8 9 12
 $ POSITION   : num  2 -3 -2 -2 -18 -3 -5 1 -5 1
 $ prix       : num  703 -245 -166 -2274 -24876 ...
> x <- rbind(avprix16[,1:3], pose15, pose16)
> x
                      DESCRIPTION       prix POSITION
2                   COPPER May/10   -716.900       -2
4                     CORN May/10  -1082.000       -3
5              COTTON NO.2 Jul/10    -79.960       -1
6           CRUDE OIL miNY May/10    -84.825       -1
7                     GOLD Jun/10   2295.500        2
9      ROBUSTA COFFEE (10) Jul/10  11084.000        8
10                  SILVER May/10  -1798.000       -1
11                SOYBEANS Jul/10   1983.500        2
12       SPCL HIGH GRADE ZINC USD  -2464.000       -1
14             SUGAR NO.11 Jul/10     32.390        2
15                   WHEAT Jul/10   2448.250        5
16                   WHEAT May/10  -2881.500       -6
1                 COFFEE C Jul/10      4.750        0
3                     CORN Jul/10     -1.500        0
8   HENRY HUB NATURAL GAS\nMay/10     -0.049        0
13              STANDARD LEAD USD   -118.000        0
17                    CORN May/10   1089.750        3
18             COTTON NO.2 Jul/10    328.480        4
19          CRUDE OIL miNY May/10    256.530        3
20     ROBUSTA COFFEE (10) Jul/10  13860.000       10
21                  SILVER May/10   1843.300        1
22                SOYBEANS Jul/10    993.000        1
23             SUGAR NO.11 Jul/10     51.240        3
24            SUGAR NO.11\nMay/10    -16.850       -1
25                   WHEAT May/10   2881.500        6
110                 COPPER May/10    702.900        2
26             COTTON NO.2 Jul/10   -244.770       -3
31          CRUDE OIL miNY May/10   -166.480       -2
41                    GOLD Jun/10  -2273.800       -2
52     ROBUSTA COFFEE (10) Jul/10 -24876.000      -18
61                SOYBEANS Jul/10  -2985.000       -3
71             SUGAR NO.11 Jul/10    -80.900       -5

On Tue, May 11, 2010 at 8:07 AM, arnaud Gaboury <arnaud.gaboury at gmail.com>
wrote:
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.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list