I feel that when I hear from one of the more outstanding members
of the list, I'm duty-bound to pass that wisdom on, and so I shall.
Thanks, Dr. Ripley! Again, many thanks to all in contributing to my
knowledge!
Note that my original question was misleading: I want the columns
that contain any negative values, instead of columns containing all
negative values. No mater, though: all the examples can be altered to do
that.
Kim Elmore
>From ripley@stats.ox.ac.uk Fri Sep 24 08:36:06 1999
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
On Thu, 23 Sep 1999, Kim Elmore wrote:
>
> I have a very simple operation that I'd like to vectorize.
> However, I haven't figured out how to do it. Being an unrepentant Fortran
> 77 hacker, this vectorization stuff sometimes escapes me. Here's what I'm
> after: within a data frame, I want to know what columns contain negative
> values and I want the results returned as a logical vector.
>
> This works, but uses looping:
>
> test <- logical(0)
> for (i in 1:100)
> {
> test[i] <- all(df[,i] < 0)
> }
So you want the columns that contain only negative values?
lapply(df, function(x) all(x < 0))
is probably as fast as any. It will give a list, so use unlist() on it.
If you had a matrix M
rep(1, nrow(M)) %*% (M >=0) == 0
would probably be faster. Under 3.4:
M <- matrix(rnorm(100*1000, -3), 1000, 100)
unix.time(rep(1, nrow(M)) %*% (M >=0) == 0)
[1] 0.22000003 0.02000001 0.00000000 0.00000000 0.00000000
unix.time(unlist(lapply(as.data.frame(M), function(x) all(x < 0))))
[1] 1.36000061 0.01000005 2.00000000 0.00000000 0.00000000
M <- as.data.frame(M)
unix.time(rep(1, nrow(M)) %*% (M >=0) == 0)
[1] 0.3300002 0.0300000 0.0000000 0.0000000 0.0000000
unix.time(unlist(lapply(M, function(x) all(x < 0))))
[1] 0.10999990 0.00999999 0.00000000 0.00000000 0.00000000
and under 5.1:
[1] 0.16 0.01 1.00 0.00 0.00
[1] 0.53 0.04 1.00 0.00 0.00
[1] 0.35 0.04 1.00 0.00 0.00
[1] 0.28 0.00 1.00 0.00 0.00
so there is not very much in it, and my guesses are correct.
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|