我想要制作400个样本,其中每个样本包含来自我的数据集的50个值。我想在for-loop中进行,但我不知道该怎么做。 以下是我目前的代码
set.seed(1)
alldata = humangenes$GC ### I am storing the value of GC variable
truemean = mean(alldata)
nsamples = 400 ###number of samples I want to make
r = data.frame()
for (j in 1:nsamples) {
mysample = sample(alldata, 50, replace = F) # Each gene can
only be picked once
##I don't know what to do next
}
建议将不胜感激。
答案 0 :(得分:1)
如果你想使用一个循环,你可以获得像这样的矩阵结果
set.seed(1)
alldata <- rnorm(1000, 1,5) # create sample data
nsamples <- 400 #number of samples I want to make
sampSize <- 50 #size of each sample
r = <- matrix(nrow = nsamples, ncol = sampSize)
for (j in 1:nsamples) {
r[j,] <- sample(alldata, sampSize, replace = FALSE)
}
然后可以通过执行
将其转换为数据帧df <- data.frame(r)
正如Zheyuan在评论中指出,循环可以替换为
r <- replicate(nsamples, sample(alldata, sampSize, replace = F))