[R] sampling and testing

Peter Alspach PAlspach at hortresearch.co.nz
Wed Nov 12 22:59:21 CET 2008


Kia ora Grant

The problem is that

feb[sample(nrow(feb),###),]

randomly rearrangements the rows of all columns together, and hence
doesn't altering the pairing.  Try

cor(feb[, col1], feb[sample(nrow(feb)), col2])

HTH ....

Peter Alspach
 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Grant Humphries
> Sent: Thursday, 13 November 2008 8:57 a.m.
> To: r-help at r-project.org
> Subject: [R] sampling and testing
> 
> Hello everyone,
> 
> I have a dataset in the following format:
> 
> col1  col2
> #     #
> #     #
> #     #
> #     #
> #     #
> #     #
> 
> 
> What I want to do is:
> loop a random sample 10 times, and for each time it is 
> sampled I want to run a correlation between both columns.  
> What I have so far is this:
> 
> >feb <- read.csv("corr.csv")  # where the dataset is for February
> >attach(feb)
> >for(i in 1:10){
> >    feb[sample(nrow(feb),###),]  # "###" represents the 
> number of rows
> >    print(cor(col1,col2),)
> >}
> 
> 
> all this is doing for me is doing 10 correlations between all 
> 10 samples at the same time.  What I was thinking was to do 
> something like:
> - append a sample of the data: sample1 <- sample()
> - do the correlation cor()
> - remove that sample using rm(sample1) function
> - repeat 9 more times
> 
> 
> any help is greatly appreciated
> 
> Cheers,
> Grant Humphries
> 
> 
> University of Alaska Fairbanks
> -EWHALE-lab Irving I  Rm 419
> 902 Koyukuk Dr.
> Fairbanks, Alaska
> 99775
> 907-474-7959
> ftgrh at uaf.edu
> 
> ______________________________________________
> 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.
> 

The contents of this e-mail are privileged and/or confidential to the named
 recipient and are not to be used by any other person and/or organisation.
 If you have received this e-mail in error, please notify the sender and delete
 all material pertaining to this e-mail.



More information about the R-help mailing list