如何在 R 中划分一组值(向量)
我正在使用 R 进行编程。我有一个包含 1000 个值的向量。现在假设我想将这 1000 个值随机划分为两个新集合,一组包含 400 个值,另一个包含 600 个值。我该怎么做?我想过做这样的事情......
firstset <- sample(mydata, size=400)
但这不会对数据进行分区(换句话说,我仍然不知道将哪 600 个值放入另一组中)。我还考虑过从 1 到 400 循环,一次随机删除 1 个值并将其放入 firstset
中。这将正确分区数据,但我不清楚如何实现这一点。另外,我被告知要尽可能避免 R 中的 for
循环。
有什么想法吗?
I'm programming in R. I've got a vector containing, let's say, 1000 values. Now let's say I want to partition these 1000 values randomly into two new sets, one containing 400 values and the other containing 600. How could I do this? I've thought about doing something like this...
firstset <- sample(mydata, size=400)
...but this doesn't partition the data (in other words, I still don't know which 600 values to put in the other set). I also thought about looping from 1 to 400, randomly removing 1 value at a time and placing it in firstset
. This would partition the data correctly, but how to implement this is not clear to me. Plus I've been told to avoid for
loops in R whenever possible.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以对它们的位置进行采样,而不是对值进行采样。
编辑:ucfagls的建议会更有效(特别是对于较大的向量),因为它避免了在R中分配位置向量。
Instead of sampling the values, you could sample their positions.
EDIT: ucfagls' suggestion will be more efficient (especially for larger vectors), since it avoids allocating a vector of positions in R.
只需随机化 mydata 并取前 400 个,然后取最后 600 个。
Just randomize mydata and take the first 400 and then last 600.
如果 mydata 确实是一个向量,则一种选择是:
If
mydata
is truly a vector, one option would be: