0

(I'm new to this so now editing my question as a reproducible example).

I've reviewed bootstrapping,loop and replicate functions and can't figure out how to repeat a series of steps (not just a single function) in R and store the result in dataframe. I need to randomly select 2 values from a pool of 22 values, 9 times. Then conduct a spearman rank correlation test on that dataset (2 columns, 9 rows) 10,000 times and store the value of each of those iterations. So I need to repeat these steps below 10,000 times and store each spearman rank outcome.

#For each isotope (C,N,S) obtain two samples of nine individuals extracted
#at random from the pool of the studied population, n = 22 nestlings) and 
#compare their isotopic values with a Spearman rank correlation. 

# take a random sample of size 2 (9 times) from a dataset mysample
# sample without replacement

c13 = c(-25.12, -20.95, -23.98, -23.78,-25.45, -26.27, -11.13, -12.75, -18.77, -18.38, -16.65,
        -16.96, -16.71, -19.57, -20, -23.19, -17.38, -17.83, -18.86, -18.71, -25.57, -21.9)

n15 = c(10.22, 12.64, 11.06, 10.81, 11.55, 11.28, 16.37, 16.17, 13.52, 13.83, 14.27, 14.07, 14.25, 13.09,
        12.59, 11.42, 13.97, 13.77, 14, 15.21, 11.73, 11.8)

s34 =c (4.61, 12.35, 5.19, 5.54, 5.2, 5.12, 14.42, 14.56,
        12.78, 13.11, 18.78, 18.71, 19.19, 11.58, 11.08, 7.89, 17.51, 17.34, 12.55, 12.65, 6.42, 8.49)

df = data.frame(c13,n15,s34)

#c13
mysample <- matrix(sample(c13, 18), ncol=2)
 
#mysample is a vector
is.atomic(mysample)
  
#Convert vector to a dataframe for correlation test.
mysample <- as.data.frame(mysample)
is.atomic(mysample)
 
#name columns
colnames(mysample) <- c ("siblingcarbon1", "siblingcarbon2")
 
#Conduct a Spearman rank correlation test on these randomly selected values 
cor.test(mysample$siblingcarbon1,mysample$siblingcarbon2, method="spearman")
  
# For c13 repeat the prior process 10,000 times and store Spearman rank value for each run (not sure how to do this)
    
StupidWolf
  • 34,518
  • 14
  • 22
  • 47
EMC
  • 3
  • 1
  • 4
  • It's easier to help if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data so we can copy/paste the code into R to run it. – MrFlick May 26 '17 at 19:21
  • 1
    Isn't that what `mysample` is, @MrFlick? I think what EMC is missing is expected output. (EMC, what have you tried? You can use `replicate(n, { expr; })` or any of `sapply`, `lapply`, `apply`, etc, along with [lists from dataframes](http://stackoverflow.com/a/24376207/3358272). And for the record, a single function is merely a parameterized series of steps ... with other programming benefits such as namespaces, scope, etc.) – r2evans May 26 '17 at 19:24
  • make it a function? – RobertMyles May 26 '17 at 19:25
  • @r2evans Where does `baea$C13` come from? I can't seem to run that. – MrFlick May 26 '17 at 19:25
  • @MrFlick, good point ... I had *assumed* (my bad) valid sample data would work with any set of numbers in place of that, but you are right that EMC should qualify what that set looks like. – r2evans May 26 '17 at 19:26

1 Answers1

0

Update the answer based on your edits:

c13 = c(-25.12, -20.95, -23.98, -23.78,-25.45, -26.27, -11.13, -12.75, -18.77, -18.38, -16.65, -16.96, -16.71, -19.57, -20, -23.19, -17.38, -17.83, -18.86, -18.71, -25.57, -21.9)
n15 = c(10.22, 12.64, 11.06, 10.81, 11.55, 11.28, 16.37, 16.17, 13.52, 13.83, 14.27, 14.07, 14.25, 13.09, 12.59, 11.42, 13.97, 13.77, 14, 15.21, 11.73, 11.8)
s34 =c (4.61, 12.35, 5.19, 5.54, 5.2, 5.12, 14.42, 14.56, 12.78, 13.11, 18.78, 18.71, 19.19, 11.58, 11.08, 7.89, 17.51, 17.34, 12.55, 12.65, 6.42, 8.49)
df = data.frame(c13,n15,s34)

#create a list to store your results
lst <- list()

#this statement does the repetition (looping)
for(i in 1:1000)
{

  mysample <- matrix(sample(c13, 18), ncol=2)
  #mysample is a vector 
  is.atomic(mysample)
  #Convert vector to a dataframe for correlation test. 
  mysample <- as.data.frame(mysample) 
  is.atomic(mysample)
  #name columns 
  colnames(mysample) <- c ("siblingcarbon1", "siblingcarbon2")
  #Conduct a Spearman rank correlation test on these randomly selected values 
  x <- cor.test(mysample$siblingcarbon1,mysample$siblingcarbon2, method="spearman")
  print(x$estimate)
  lst[i] <- x$estimate
}
str(lst)

The results will be stored in the order run in lst. Ran it 10 times (instead of 1,000):

str(lst)

List of 10
 $ : num -0.5
 $ : num -0.1
 $ : num 0.15
 $ : num -0.8
 $ : num 0.0167
 $ : num -0.617
 $ : num 0.183
 $ : num -0.617
 $ : num 0.2
 $ : num 0.05
PhilC
  • 697
  • 3
  • 7
  • Thank you for the help! I've updated to include the data and tried the code from PhilC. After reviewing the lst by str(lst) I still am not clear if it is giving me a list of the correlation coefficients (rho)? – EMC Jun 01 '17 at 17:27
  • Please try the updated code above and mark as answered if appropriate. – PhilC Jun 01 '17 at 18:58