如何重复代码块以对r中的2个值进行采样? [英] How to repeat a block of code to sample 2 values in r?

查看:63
本文介绍了如何重复代码块以对r中的2个值进行采样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是我的新手,所以现在将我的问题编辑为可重复的示例).

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

我已经审查了自举,循环和复制功能,无法弄清楚如何在R中重复一系列步骤(而不仅仅是单个功能)并将结果存储在数据框中.我需要从22个值的池中随机选择2个值,进行9次.然后对该数据集(2列9行)进行10,000次spearman等级相关性测试,并存储每个迭代的值.因此,我需要在10,000次以下重复这些步骤,并存储每个矛兵排名结果.

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)
    

推荐答案

根据您的修改来更新答案:

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)

结果将按照在lst中运行的顺序存储.运行10次(而不是1000次):

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

str(lst)

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

这篇关于如何重复代码块以对r中的2个值进行采样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆