[R] [Fwd: Excluding data with apply]
Christian Kamenik
christian.kamenik at giub.unibe.ch
Fri Jan 9 09:30:51 CET 2009
Dear all,
I've got many responses to my initial question, which is stated below.
However, from those responses it has become clear that I need to
rephrase my problem. All responses dealt with subscripting the data
matrix before 'apply' is run on it. But this is not want I wanted to do.
'apply' cycles through rows or columns of a matrix, and runs a function
on each row or column individually. Now, instead of focusing on each
individual row or column, I want to get rid of these rows or columns,
and run the function on the remaining matrix.
I could do this with a for loop, such as:
x<-matrix(rnorm(100),20,5)
for (i in 1:ncol(x)) print(mean(x[,-i]))
But for more complex problems this becomes tedious...
Any ideas would be highly appreciated, Christian
>
> Dear all,
>
> 'Apply' is a great thing for running functions on rows or columns of a
> matrix:
>
> X <- rnorm(20, mean = 0, sd = 1)
> dim(X) <- c(5,4)
> apply(X,2,sum)
>
> Is there a way to use apply for excluding rows or columns from a matrix
> to run functions on the remaining rows or columns? I know, I could do
> this with a 'for' loop, but 'apply' would be much easier and quicker,
> and require less programming...
>
> Cheers, Christian
>
--
-----------------------------------------------
Oeschger Centre for Climate Change Research, Institute of Geography & Institute of Plant Sciences
University of Bern
http://www.stomatocysts.unibe.ch/kamenik
mailto: christian.kamenik at giub.unibe.ch
Postal address:
Dr. Christian Kamenik
Institute of Geography
Erlachstrasse 9a, Trakt 3
3012 Bern, Switzerland
Tel. +41 (0)31 631 5091
Fax +41 (0)31 631 43 38
More information about the R-help
mailing list