[R] Alternatives to unlist()

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri May 4 19:48:42 CEST 2007



Jacques Wagnor said the following on 5/4/2007 8:53 AM:
> Given the following, one of the things I am trying to see is what % of
> draws are below a certain number:
> 
> lambda <- 3
> rate <- 5
> n <- 5
> 
> set.seed(123)
> v <- replicate(n, rexp(rpois(1,lambda), rate))
> vv <- unlist(v)
> cat("% of draws below 0.1:", round(length(subset(vv, vv <
> 0.1))/length(vv)*100,0), "%\n")
> 
> In actuality, my lambda, rate, and n are 26, 10, 1000000,
> respectively; which in effect makes the length of vv roughly equal
> 26'000'000. When I run cat(...), I get the following:
> 
> Error: cannot allocate vector of size 101540 Kb
> In addition: Warning messages:
> 1: Reached total allocation of 1015Mb: see help(memory.size)
> 2: Reached total allocation of 1015Mb: see help(memory.size)
> 
> Rather than keep the code as is and resort to memory.limit(), I would
> like to see how the code can be modified (i.e., alternatives to
> unlist()) such that I could still see what % of draws are below a
> certain number.
> 
> I'd appreciate any suggestions.
> 
> Regards,
> 
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    2.1
> year     2005
> month    12
> day      20
> svn rev  36812
> language R
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

This works for me on R-2.5.0 on Win XP with 1GB RAM.

system.time({
   set.seed(1)
   lambda <- 26
   rate <- 10
   n <- 1000000
   v <- replicate(n, rexp(rpois(1, lambda), rate))
   vbar <- sapply(v, function(x) mean(x < 0.1))
   vlen <- sapply(v, length)
})
#   user  system elapsed
#  75.67    1.13   96.85

(m <- weighted.mean(vbar, vlen))
# [1] 0.6320496

cat("% of draws below 0.1: ", round(m * 100), "%\n", sep = "")

Note your version R is almost a year and half old. Please upgrade. It's 
easy and free.

--sundar



More information about the R-help mailing list