s-news
[Top] [All Lists]

Re: Generating a vector dynamically

To: Tony Plate <tplate@blackmesacapital.com>
Subject: Re: Generating a vector dynamically
From: David L Lorenz <lorenz@usgs.gov>
Date: Fri, 21 Apr 2006 07:30:19 -0500
Cc: "Khan, Sohail" <khan@cshl.edu>, s-news@lists.biostat.wustl.edu, s-news-owner@lists.biostat.wustl.edu
In-reply-to: <444823C6.3070503@blackmesacapital.com>

Hi,
  It is probably a little more efficient to take all of the samples and then process them. So another way:

res <- colSums(matrix(sample(d$x, 10*1000, rep=T), ncol=1000))

Dave


Tony Plate <tplate@blackmesacapital.com>
Sent by: s-news-owner@lists.biostat.wustl.edu

04/20/2006 07:13 PM

To
"Khan, Sohail" <khan@cshl.edu>
cc
s-news@lists.biostat.wustl.edu
Subject
Re: [S] Generating a vector dynamically





Here's one way to do what I think you want:

> set.seed(1)
> # Generate a sample data frame with one column 'x'
> d <- data.frame(x=sample(c(10:14,100),50,rep=T))
> # Create a vector with the sum of 1000 samples of size
> # 10 (with replacement)
> res <- sapply(1:1000, function(i) sum(sample(d$x, 10, rep=T)))
> hist(res)
>

Khan, Sohail wrote:
> Dear List
>
> I want to write a "little" piece of  code which would :
>
> -- take sample of 10 values (randomly) from a data.frame column
> -- sum these values
> -- put the values in a vector
> -- repeat 1000 times
>
> I would later draw a histogram of this generated vector.
> Thanks in advance for any advice/suggestions.
>
> Sohail Khan
> Scientific Programmer
> COLD SPRING HARBOR LABORATORY
> Genome Research Center
> 500 Sunnyside Boulevard
> Woodbury, NY 11797
> (516)422-4076
>
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
>

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>