I am looking for a more general solution to below exercise. Thanks, Naresh library(plyr) mydf <- data.frame( date = rep(seq.Date(from = as.Date("2022-06-01"), by = 1, length.out 10), 4), account = c(rep("ABC", 20), rep("XYZ", 20)), client = c(rep("P", 10), rep("Q", 10), rep("R", 10), rep("S", 10)), profit = round(runif(40, 2, 5), 2), sales = round(runif(40, 10, 20), 2)) mydf.split <- split(mydf, mydf$account) # if there are 10 variables like sales, profit, etc., need 10 lines myres <- lapply(mydf.split, function(df) { sales.ts <- aggregate(sales ~ date, FUN = sum, data = df) #one step for both? profit.ts <- aggregate(profit ~ date, FUN = sum, data = df) merge(profit.ts, sales.ts, by = "date")}) myres.df <- ldply(myres)
Hi Maybe lapply(mydf.split, function(x) aggregate(x[,4:5], list(x$date), sum)) For your particular case, but lacking overall genarality. Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Naresh Gurbuxani > Sent: Friday, July 1, 2022 1:08 PM > To: r-help at r-project.org > Subject: [R] split apply on multiple variables > > > I am looking for a more general solution to below exercise. > > Thanks, > Naresh > > library(plyr) > mydf <- data.frame( > date = rep(seq.Date(from = as.Date("2022-06-01"), by = 1, length.out > 10), 4), > account = c(rep("ABC", 20), rep("XYZ", 20)), > client = c(rep("P", 10), rep("Q", 10), rep("R", 10), rep("S", 10)), > profit = round(runif(40, 2, 5), 2), sales = round(runif(40, 10, 20), 2)) > > mydf.split <- split(mydf, mydf$account) > > # if there are 10 variables like sales, profit, etc., need 10 lines > myres <- lapply(mydf.split, function(df) { > sales.ts <- aggregate(sales ~ date, FUN = sum, data = df) #one step forboth?> profit.ts <- aggregate(profit ~ date, FUN = sum, data = df) > merge(profit.ts, sales.ts, by = "date")}) > > myres.df <- ldply(myres) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
I have solved this problem. ddply(mydf, c("date", "account"), function(df) { profit = sum(profit); sales = sum(sales); data.frame(profit = profit, sales = sales)}) From: R-help <r-help-bounces at r-project.org> on behalf of Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> Sent: Friday, July 1, 2022 7:07 AM To: r-help at r-project.org <r-help at R-project.org> Subject: [R] split apply on multiple variables ? I am looking for a more general solution to below exercise. Thanks, Naresh library(plyr) mydf <- data.frame( date = rep(seq.Date(from = as.Date("2022-06-01"), by = 1, length.out 10), 4), account = c(rep("ABC", 20), rep("XYZ", 20)), client = c(rep("P", 10), rep("Q", 10), rep("R", 10), rep("S", 10)), profit = round(runif(40, 2, 5), 2), sales = round(runif(40, 10, 20), 2)) mydf.split <- split(mydf, mydf$account) # if there are 10 variables like sales, profit, etc., need 10 lines myres <- lapply(mydf.split, function(df) { sales.ts <- aggregate(sales ~ date, FUN = sum, data = df) #one step for both? profit.ts <- aggregate(profit ~ date, FUN = sum, data = df) merge(profit.ts, sales.ts, by = "date")}) myres.df <- ldply(myres) ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.