[R] Calculate of data frame
Hello, I have a dataframe which I read from a file: df - readtable(myFile); The dataframe has 4 columns: model, count, value and date where model and date are made of charactors and count and value are numbers. An example is like: modelcountvaluedate A420.87/1/2005 A310.27/2/2005 B714.27/1/2005 B16.2 7/2/2005 A2117/1/2005 B965.2 7/2/2005 Now I want to get the total count and value for each model/date pair, like modelcountvaluedate A631.87/1/2005 A310.27/2/2005 B714.27/1/2005 B1067.4 7/2/2005 Anyone can tell me how to do this? Thanks. Fan [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Calculate of data frame
Zhang, Fan [EMAIL PROTECTED] writes: Now I want to get the total count and value for each model/date pair, like modelcountvaluedate A631.87/1/2005 A310.27/2/2005 B714.27/1/2005 B1067.4 7/2/2005 Anyone can tell me how to do this? aggregate(df[,c(count,value)],df[,c(date,model)],sum) date model count value 1 7/1/2005 A 6 31.8 2 7/2/2005 A 3 10.2 3 7/1/2005 B 7 14.2 4 7/2/2005 B10 71.4 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Calculate of data frame
Thanks for your answer, Peter. Now I have a new data frame which is sorted by (model,date) pair. Since the sorting is done to charactors, I have 7/1/2005 followed by 7/10/2005 instead of 7/2/2005. How can I do the aggregation and at the same time, make the result sorted the way I want (i.e, 7/1/2005 followd by 7/2/2005)? Thanks, Fan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard Sent: Thursday, July 14, 2005 1:29 PM To: Zhang, Fan Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Calculate of data frame Zhang, Fan [EMAIL PROTECTED] writes: Now I want to get the total count and value for each model/date pair, like modelcountvaluedate A631.87/1/2005 A310.27/2/2005 B714.27/1/2005 B1067.4 7/2/2005 Anyone can tell me how to do this? aggregate(df[,c(count,value)],df[,c(date,model)],sum) date model count value 1 7/1/2005 A 6 31.8 2 7/2/2005 A 3 10.2 3 7/1/2005 B 7 14.2 4 7/2/2005 B10 71.4 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html