[R] Apply Function to List by Group Returning Result with Dim of List

David Winsemius dwinsemius at comcast.net
Fri Jan 20 03:15:43 CET 2012


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



More information about the R-help mailing list