How to repeat a block of code to sample 2 values in r?

百般思念 提交于 2021-02-05 11:28:07

问题


(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)
    

回答1:


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


来源:https://stackoverflow.com/questions/44208676/how-to-repeat-a-block-of-code-to-sample-2-values-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!