将数据帧中的随机行分配到 R 中的其他 2 个数据帧 [英] Assigning random rows from a dataframe into 2 other dataframes in R

查看:44
本文介绍了将数据帧中的随机行分配到 R 中的其他 2 个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所述的数据框 (a):

I have a dataframe (a) as mentioned below:

   V1 V2
1   a  b
2   a  e
3   a  f
4   b  c
5   b  e
6   b  f
7   c  d
8   c  g
9   c  h
10  d  g
11  d  h
12  e  f
13  f  g
14  g  h

现在我想要的是将上述数据帧 (a) 中的行随机分配给其他 2 个空数据帧(b 和 c),这样所有行都不会重复.这意味着 b 没有任何重复的行, c 也没有任何重复的行.现在除了 b 和 c 之外,任何行都不应该相同,即 b 中的一行不应出现在 c 的任何行中,反之亦然.

Now what i want is to randomly assign rows from the above dataframe (a) to 2 other empty dataframes (b and c) such that none of the rows are repeated. That means neither b has any repeated rows nor c has any repeated row. Now apart from that even across b and c, none of the rows should be same i.e a row in b shouldn't be present in any rows of c and vice versa.

有一种方法是从(a)中抽取 7 个元素而不进行替换并分配给(b),然后将剩余的分配给(c).但是在这种方法中,所有元素都将同时分配给(b)然后分配给(c)但我想要的是一个一个地分配元素.这是到 (b) 的随机行,然后是到 (c) 的随机行,然后又是到 (b) 的随机行......依此类推,直到数据帧 (a) 中的所有行都完成.

Once way is to sample 7 elements from (a) without replacement and assign to (b) and then assign remaining to the (c). But in this approach all elements would be assigned at the same time to (b) and then to (c) BUT what i want is to assign elements one by one. That is a random row to (b) then a random row to (c) then again a random row to (b) ... and so on till all rows in dataframe (a) are done.

谢谢

推荐答案

对所有行号进行采样,然后根据行号索引的奇偶校验对数据帧进行分区应该可以实现您的目标.这与逐行随机分区原始数据帧相同.

Sampling all of the row numbers and then partitioning the dataframe according to the parity of the row number indexes should achieve what you are after. This is the same as randomly partitioning the original dataframe row-by-row.

n <- nrow(df)
s <- sample.int(n, n)
odd.idxs <- seq_along(s) %% 2 != 0

s1 <- s[odd.idxs]
s2 <- s[-odd.idxs]

d1 <- df[s1, ]
d2 <- df[s2, ]

这篇关于将数据帧中的随机行分配到 R 中的其他 2 个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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