Hi, I wonder if someone can suggest how to create a new data.frame Y from X where X$PL_Pos is summed by each unique X$MyDate. Y should end up with two (or more) columns Y$MyDate and Y$PL_Sum with its value being the cumsum of all the values in X for that date. - a 'daily cumsum'. Thanks, Mark TStoDate = function (TSDate) { X = strptime(TSDate + 19e6L, "%Y%m%d") return(as.Date(X)) } X = structure(list(EnDate = c(1090803, 1090804, 1090805, 1090806, 1090806, 1090810, 1090811, 1090812, 1090813, 1090817, 1090819, 1090820, 1090820, 1090824, 1090825, 1090825, 1090826, 1090826, 1090827, 1090827, 1090827), PL_Pos = c(174, -26, 614, 344, -26, 414, -626, 544, -106, -146, 1004, 344, 224, -716, -176, 44, 354, -346, -296, 564, 354)), .Names = c("EnDate", "PL_Pos"), class = "data.frame", row.names = c("733", "734", "3631", "3641", "736", "2403", "2413", "3651", "3661", "3671", "3681", "3691", "1303", "3701", "1304", "1305", "2432", "1306", "3712", "1307", "4214")) X$MyDate = TStoDate(X$EnDate) X
Is this what you want:> aggregate(X$PL_Pos, list(X$MyDate), sum)Group.1 x 1 2009-08-03 174 2 2009-08-04 -26 3 2009-08-05 614 4 2009-08-06 318 5 2009-08-10 414 6 2009-08-11 -626 7 2009-08-12 544 8 2009-08-13 -106 9 2009-08-17 -146 10 2009-08-19 1004 11 2009-08-20 568 12 2009-08-24 -716 13 2009-08-25 -132 14 2009-08-26 8 15 2009-08-27 622>You can fix up the column names. On Fri, Aug 28, 2009 at 12:37 PM, Mark Knecht<markknecht at gmail.com> wrote:> Hi, > ? I wonder if someone can suggest how to create a new data.frame Y > from X where X$PL_Pos is summed by each unique X$MyDate. Y should end > up with two (or more) columns Y$MyDate and Y$PL_Sum with its value > being the cumsum of all the values in X for that date. - a 'daily > cumsum'. > > Thanks, > Mark > > > TStoDate = function (TSDate) { > ? ? ? ?X = strptime(TSDate + 19e6L, "%Y%m%d") > ? ? ? ?return(as.Date(X)) > } > > X = structure(list(EnDate = c(1090803, > 1090804, 1090805, 1090806, 1090806, 1090810, 1090811, 1090812, > 1090813, 1090817, 1090819, 1090820, 1090820, 1090824, 1090825, > 1090825, 1090826, 1090826, 1090827, 1090827, 1090827), PL_Pos = c(174, > -26, 614, 344, -26, 414, -626, 544, -106, -146, 1004, 344, 224, > -716, -176, 44, 354, -346, -296, 564, 354)), .Names = c("EnDate", "PL_Pos"), > class = "data.frame", row.names = c("733", > "734", "3631", "3641", "736", "2403", "2413", "3651", "3661", > "3671", "3681", "3691", "1303", "3701", "1304", "1305", "2432", > "1306", "3712", "1307", "4214")) > > X$MyDate = TStoDate(X$EnDate) > > X > > ______________________________________________ > 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?
The request for a date column results in a bit of redundancy beyond what tapply would have produced, but here it is as specified: > data.frame(dnames=names(tapply(X$PL_Pos, X$MyDate, sum)), dsums = tapply(X$PL_Pos, X$MyDate, sum) ) dnames dsums 2009-08-03 2009-08-03 174 2009-08-04 2009-08-04 -26 2009-08-05 2009-08-05 614 2009-08-06 2009-08-06 318 2009-08-10 2009-08-10 414 2009-08-11 2009-08-11 -626 2009-08-12 2009-08-12 544 2009-08-13 2009-08-13 -106 2009-08-17 2009-08-17 -146 2009-08-19 2009-08-19 1004 2009-08-20 2009-08-20 568 2009-08-24 2009-08-24 -716 2009-08-25 2009-08-25 -132 2009-08-26 2009-08-26 8 2009-08-27 2009-08-27 622 On Aug 28, 2009, at 12:37 PM, Mark Knecht wrote:> Hi, > I wonder if someone can suggest how to create a new data.frame Y > from X where X$PL_Pos is summed by each unique X$MyDate. Y should end > up with two (or more) columns Y$MyDate and Y$PL_Sum with its value > being the cumsum of all the values in X for that date. - a 'daily > cumsum'. > > Thanks, > Mark > > > TStoDate = function (TSDate) { > X = strptime(TSDate + 19e6L, "%Y%m%d") > return(as.Date(X)) > } > > X = structure(list(EnDate = c(1090803, > 1090804, 1090805, 1090806, 1090806, 1090810, 1090811, 1090812, > 1090813, 1090817, 1090819, 1090820, 1090820, 1090824, 1090825, > 1090825, 1090826, 1090826, 1090827, 1090827, 1090827), PL_Pos = c(174, > -26, 614, 344, -26, 414, -626, 544, -106, -146, 1004, 344, 224, > -716, -176, 44, 354, -346, -296, 564, 354)), .Names = c("EnDate", > "PL_Pos"), > class = "data.frame", row.names = c("733", > "734", "3631", "3641", "736", "2403", "2413", "3651", "3661", > "3671", "3681", "3691", "1303", "3701", "1304", "1305", "2432", > "1306", "3712", "1307", "4214")) > > X$MyDate = TStoDate(X$EnDate) > > X > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT