jamesrobinsonjnr
2012-Jan-19 23:42 UTC
[R] Apply Function to List by Group Returning Result with Dim of List
I have a panel data set defined as a simple data.frame with a factor age and income. I would like to generate the results of a set function such as (sum, mean, or even diff) separate for each factor (category or age group). However I want the result to have the same dimension as the overal data.frame. Therefore, I expect to get repeaded the same result for each item within a category, but different accross categories. Eg: age income avg_inc 20 100 100 20 90 100 20 110 100 30 200 200 30 250 200 30 150 200 What builtin function can assist in doing this without my having to write loops. I know in Eviews there is the sumsby, meansby, minsby and so forth. -- View this message in context: http://r.789695.n4.nabble.com/Apply-Function-to-List-by-Group-Returning-Result-with-Dim-of-List-tp4311880p4311880.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Jan-20 02:15 UTC
[R] Apply Function to List by Group Returning Result with Dim of List
On Jan 19, 2012, at 6:42 PM, jamesrobinsonjnr wrote:> I have a panel data set defined as a simple data.frame with a factor > age and > income. I would like to generate the results of a set function such > as (sum, > mean, or even diff) separate for each factor (category or age group). > However I want the result to have the same dimension as the overal > data.frame. Therefore, I expect to get repeaded the same result for > each > item within a category, but different accross categories. > > Eg: > age income avg_inc > 20 100 100 > 20 90 100 > 20 110 100 > 30 200 200 > 30 250 200 > 30 150 200?ave (which returnsf the same length as the first argument.) ?tapply ?aggregate (neither which will yield that same number of items (There are also the doBy and plyr packages.)> > What builtin function can assist in doing this without my having to > write > loops. I know in Eviews there is the sumsby, meansby, minsby and so > forth.-- David Winsemius, MD West Hartford, CT
jamesrobinsonjnr
2012-Jan-20 02:42 UTC
[R] Apply Function to List by Group Returning Result with Dim of List
Excellent. Thank you very much David. Your help is greatly appreciated. "ave" worked like a charm. James -- View this message in context: http://r.789695.n4.nabble.com/Apply-Function-to-List-by-Group-Returning-Result-with-Dim-of-List-tp4311880p4312262.html Sent from the R help mailing list archive at Nabble.com.