使用R中的负下标生成样本未拾取值?

时间:2016-02-25 00:18:47

标签: r subscript

统计学生在这里。我的任务是组合两组值,然后将它们组合起来进行排列测试。在这个例子中,我将向量组合成一个,然后使用sample()函数提取一半的值来找到它们的平均值并将它与另一半进行比较。我被告知我可以使用否定下标以某种方式拉出未挑选的值并将它们放入另一个变量中,以便我可以比较这些方法。

controls <- c(6.33, 1.65, -3.58, 3.3, -6.6, 3.29, 1.80, 1.80, 2.98) ## group 1
redA <- c(6.84, -9.83, -0.02, -9.12, -0.07, -19.34, 3.97, -16.37, -21.02) ## group 2
allscores <- c(controls, redA) ## combine vectors
allscores ## check new vector
controlIDs <- sample(allscores,9) ## pull out 9 random values
otherIDs <- allscores[-controlIDs] ## get other 9 non-picked values
difference <- mean(controlIDs) - mean(otherIDs) ## compare means

这是我遇到的错误

Error in allscores[-controlIDs] : 
  only 0's may be mixed with negative subscripts

从概念上讲,当我认为样本独立运行时,我很难确定R如何使用负下标来挑选未挑选的样本值。

有人可以告诉我,如果我的代码在某种程度上混淆了吗?理想情况下,这将有效,因此可以将其放入for循环并运行多次以创建值的分布。在学习如何使用负下标将未选择的值拉出到单独的向量中时,将不胜感激。一个类调用说整个allscores向量是数字的,所以我不相信它是数据类型中的冲突,但我可能是错的。

谢谢!

1 个答案:

答案 0 :(得分:-1)

当您需要值的索引来移除它们时,您从allscores采样值时出现错误。

所以你可以改为使用:

...
idx <- sample(seq_along(allscores),9) ## take 9 random values
controlIDs <- allscores[idx] ## pull out these 9random values
otherIDs <- allscores[-idx] ## get other 9 non-picked values
...