[R] using sapply to apply function to some columns of a dataframe

2009-02-17 Thread mwestphal

Hello:

I would like to sum every x columns of a dataframe for each row.  For instance,
if x is 10, then for dataframe df, this function will sum the first ten elements
together and then the next ten:

sapply(list(colnames(df)[1:10], colnames(df)[11:20]),function(x)apply( df[,x],
1, sum))

If the number of columns is quite large (1000's), then manually entering the
list above is not practical.  Any suggestions?

I would also like to do a variant of the above, where I sum every nth element.


Thanks,

Michael

--
Michael I. Westphal, PhD
Africa Region Water Resources (AFTWR)
South Asia Region Sustainable Development (SASSD)
World Development Report 2010: Development in a Changing Climate
www.worldbank.org/wdr2010
Room J6-007 (mail stop: J6-603)
Tel: 202.473.1217
The World Bank
1818  H St NW, Washington DC 20433, USA

__
R-help@r-project.org mailing list
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.


Re: [R] using sapply to apply function to some columns of a dataframe

2009-02-17 Thread Duncan Murdoch

On 17/02/2009 4:42 PM, mwestp...@worldbank.org wrote:

Hello:

I would like to sum every x columns of a dataframe for each row.  For instance,
if x is 10, then for dataframe df, this function will sum the first ten elements
together and then the next ten:

sapply(list(colnames(df)[1:10], colnames(df)[11:20]),function(x)apply( df[,x],
1, sum))

If the number of columns is quite large (1000's), then manually entering the
list above is not practical.  Any suggestions?

I would also like to do a variant of the above, where I sum every nth element.


I think the easiest way to do this is to convert the dataframe into an 
array with 3 indices, and sum over one of them.  For example:


rows - 20
cols - 120

df - matrix(1:(rows*cols), rows, cols)
# in your case, df - as.matrix( df )

arr - array( df, c(rows, 10, cols/10))
sums - apply( arr, c(1,3), sum)

Duncan Murdoch

__
R-help@r-project.org mailing list
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.