[R] [Q] Setting elements of a vector

Setzer.Woodrow@epamail.epa.gov Setzer.Woodrow at epamail.epa.gov
Mon Nov 12 22:48:56 CET 2001


Your code samples from 1:4 20 times with replacement, sums the results,
then assigns the value to all 10000 elements of vec.  There is no way
for 'sample' to 'know' that you want 20*10000 samples!  Try the
following instead:

>  vec <-
apply(matrix(sample(1:4,20*10000,awprob,replace=TRUE),nrow=20),2,sum)

> length(vec)
[1] 10000
> vec[1:10]
 [1] 43 38 35 41 35 37 41 42 40 36
> summary(vec)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  23.00   35.00   38.00   37.93   41.00   56.00

or

> vec2 <-
tapply(sample(1:4,20*10000,awprob,replace=TRUE),rep(1:10000,rep(20,10000)),sum)
> summary(vec2)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  25.00   35.00   38.00   37.99   41.00   57.00
> length(vec2)
[1] 10000

Though on my system (R-1.3.1 on Windows 98) the latter is about 50%
slower.


R. Woodrow Setzer, Jr.                                            Phone:
(919) 541-0128
Experimental Toxicology Division                       Fax:  (919)
541-5394
Pharmacokinetics Branch
NHEERL MD-74; US EPA; RTP, NC 27711


                                                                                                               
                    Stefan Meyer                                                                               
                    <stem at stem.de>              To:     r-help at lists.R-project.org                             
                    Sent by:                    cc:                                                            
                    owner-r-help at stat.ma        Subject:     [R] [Q] Setting elements of a vector              
                    th.ethz.ch                                                                                 
                                                                                                               
                                                                                                               
                    11/12/01 03:35 PM                                                                          
                                                                                                               
                                                                                                               




Hi!

I'm new to R and have problems with the following code:

<code>

awprob<-c(0.4,0.4,0.1,0.1)
awsample<-sample(c(1:4),3,awprob,replace=TRUE)

vec<-vector(mode="numeric",10000)
vec[]<-sum(sample(c(1:4),20,awprob,replace=TRUE))

</code>

My problem: The elements of vec have the same value. How
can I tell R that I want to have a *new* sample for each value
and not 10000 times the same sample?

A for-loop is not a good solution as it made R hang up ...

Thanx alot

Stefan

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-
r-help mailing list -- Read
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._




-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list