如何在 R 中划分一组值(向量)

发布于 2024-09-26 17:29:03 字数 362 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟织青萝梦 2024-10-03 17:29:03

您可以对它们的位置进行采样,而不是对值进行采样。

positions <- sample(length(mydata), size=400)  # ucfagls' suggestion
firstset <- mydata[positions]
secondset <- mydata[-positions]

编辑:ucfagls的建议会更有效(特别是对于较大的向量),因为它避免了在R中分配位置向量。

Instead of sampling the values, you could sample their positions.

positions <- sample(length(mydata), size=400)  # ucfagls' suggestion
firstset <- mydata[positions]
secondset <- mydata[-positions]

EDIT: ucfagls' suggestion will be more efficient (especially for larger vectors), since it avoids allocating a vector of positions in R.

暖阳 2024-10-03 17:29:03

只需随机化 mydata 并取前 400 个,然后取最后 600 个。

mydata <- sample(mydata)
firstset <- mydata[1:400]
secondset <- mydata[401:1000]

Just randomize mydata and take the first 400 and then last 600.

mydata <- sample(mydata)
firstset <- mydata[1:400]
secondset <- mydata[401:1000]
时光病人 2024-10-03 17:29:03

如果 mydata 确实是一个向量,则一种选择是:

split(mydata, sample(c(rep("group1", 600), rep("group2", 400))))

If mydata is truly a vector, one option would be:

split(mydata, sample(c(rep("group1", 600), rep("group2", 400))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文