[R] Odp: merge two data frames

Petr PIKAL petr.pikal at precheza.cz
Wed Aug 4 12:02:41 CEST 2010


Hi

you tried OK

result <- merge(zz, av, by="DESCRIPTION", all=TRUE)

and as you did not specify what to do when one value is NA  here is one 
possible solution

rowSums(cbind(result$PL.x, result$PL.y), na.rm=T)

Regards
Petr

r-help-bounces at r-project.org napsal dne 04.08.2010 11:52:00:

> Dear list,
> 
> here are my two data frames:
> 
> av <-
> structure(list(DESCRIPTION = c("COFFEE C Sep/10", "COPPER Sep/10",
> "CORN Dec/10", "CRUDE OIL miNY Sep/10", "GOLD Aug/10", "HENRY HUB
> NATURAL GAS Sep/10",
> "PALLADIUM Sep/10", "SILVER Sep/10", "SOYBEANS Nov/10", "SPCL HIGH
> GRADE ZINC USD",
> "SUGAR NO.11 Oct/10", "WHEAT Sep/10"), prix = c(-168.3, 
-1.60000000000002,
> -773.75, -78.75, -1168.3, -0.0919999999999996, -470.75, 1758.5,
> -975.25, 1964, -19.09, -605.75), pos = c(-1, 0, -2, -1, -1, 0,
> -1, 1, -1, 1, -1, -1), PL = c(-12.03, -31.68, -43.2, -70.49,
> -11.88, -95.04, -3.96, -35.64, -30.24, -12.5, -36.09, -4.32)), .Names
> = c("DESCRIPTION",
> "prix", "pos", "PL"), row.names = c(NA, 12L), class = "data.frame")
> 
> zz <-
> structure(list(DESCRIPTION = structure(c(1L, 2L, 3L, 4L, 5L,
> 6L, 7L, 8L, 9L, 10L, 13L, 11L, 12L), .Label = c("COFFEE C Sep/10",
> "COPPER Sep/10", "CORN Dec/10", "CRUDE OIL miNY Sep/10", "GOLD Aug/10",
> "HENRY HUB NATURAL GAS Sep/10", "PALLADIUM Sep/10", "PRM HGH GD 
ALUMINIUM USD",
> "SILVER Sep/10", "SOYBEANS Nov/10", "SPCL HIGH GRADE ZINC USD",
> "SUGAR NO.11 Oct/10", "WHEAT Sep/10"), class = "factor"), pl =
> c(-8.20000000000002,
> 2.30000000000001, -47.25, -0.230000000000004, -12.7000000000000,
> -0.236, -2, 11.7100000000000, 14.4000000000001, -34.75, -10.75,
> 55, -0.669999999999998), PL = c(3075.00000000001, -575.000000000003,
> 2362.5, 115.000000000002, 1270.00000000000, 2360, 200, 
-292.750000000001,
> -720.000000000005, 1737.5, 537.5, -1375, 750.399999999998), POSITION = 
c(1,
> -1, 2, 2, 1, 2, -1, -1, 1, 2, 0, 0, 0), SETTLEMENT = c(167.4,
> 324.55, 390.75, 76.99, 1160.4, 4.718, 468.75, 2067.71, 1744.1,
> 978, 0, 0, 0)), .Names = c("DESCRIPTION", "pl", "PL", "POSITION",
> "SETTLEMENT"), row.names = c(NA, -13L), class = "data.frame")
> 
> I am looking for one data frame with the column $PL=zz$PL+av$PL.
> I have been trying using the merge() function and its different
> arguments with no sucess.
> 
> Any help is appreciated.
> Thank You.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list