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.
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 GDALUMINIUM 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi Arnaud, It is slightly confusing what you are asking but if you just want a dataframe with the two zz$"PL" and av$"PL" columns concatenated then merge is not what you are after. Try something like this. t <- rbind(data.frame(PL=zz$PL), data.frame(PL=av$PL))> tPL 1 3075.00 2 -575.00 3 2362.50 4 115.00 5 1270.00 6 2360.00 7 200.00 8 -292.75 9 -720.00 10 1737.50 11 537.50 12 -1375.00 13 750.40 14 -12.03 15 -31.68 16 -43.20 17 -70.49 18 -11.88 19 -95.04 20 -3.96 21 -35.64 22 -30.24 23 -12.50 24 -36.09 25 -4.32 However, if you are trying to JOIN the dataframes on the PL column then you can use merge to give you this intersection...though as there aren't any intersecting values on this column, I suspect this isn't what you really want. Anyway to use merge try this: zz.av.join <- merge(zz, av, by.x="PL", by.y="PL") zz.av.join [1] PL DESCRIPTION.x pl POSITION SETTLEMENT DESCRIPTION.y prix [8] pos <0 rows> (or 0-length row.names) ------ 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. Amos Folarin (BSc MRes PhD) Research Associate, Bioinformatics Group ********************************************* UCL Cancer Institute Paul O'Gorman Building University College London 72 Huntley Street London WC1E 6BT Direct Line: +44 (0)207 679 0764 Email: a.folarin@cancer.ucl.ac.uk <mailto:a.folarin@cancer.ucl.ac.uk> ********************************************* ********************************************************************** This email and any files transmitted with it are confide...{{dropped:10}}
TY Petr, it works. I will then replace NA by 0. 2010/8/4 Petr PIKAL <petr.pikal at precheza.cz>:> 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. > >-- ****************************** Arnaud Gaboury Mobile: +41 79 392 79 56 BBM: 255B488F