[R] Excluding data with apply

David Winsemius
Thu, 08 Jan 2009 17:26:33 -0000

Using indexing and putting a minus sign in front of a vector of column
names that you want to exclude would be a typical approach:

df <- data.frame(a=LETTERS[1:4], b= rnorm(4), c=rnorm(4), d=
letters[5:9])

apply(df[ , -c("a","d")], 2, sum)

(Pretty sure this will run properly but don't have R up an runnign to
test it.)



--
David Winsemius

On Jan 8, 2009, at 5:52 AM, Christian Kamenik wrote:

> 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
>
> ______________________________________________
> R-help at 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.