[R] Means from selected columns in a data frame

David Winsemius dwinsemius at comcast.net
Tue Jul 20 15:25:02 CEST 2010


On Jul 20, 2010, at 9:13 AM, Marcus Drescher wrote:

> Hi all,
>
> I have a dataframe with survey data. Now I want to calculate means  
> from several but not all columns (e.g. a1, a2, a3) and save them in  
> a new separate column (e.g. a).

Well that would be possible unless you are in Minitab or Excel where  
you can put stuff of variable length where ever.
>
> Like: a = mean(a1, a2, a3)
>
> Can someone help me or give me the right key word to look for?

?colMeans

Perhaps:
meansvec <- colMeans(dfrm[ , c("a1", "a2", "a3")] )

If on the other hand, you wanted rowMeans (since your problems  
specification was rather ambiguous)  that function is also available.

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list