Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R将行随机分成两半_R - Fatal编程技术网

R将行随机分成两半

R将行随机分成两半,r,R,在r中,如果我随机获取数据表中的一半行,那么如何获取另一半行。我一直在使用sample_n()首先抓取随机行,这是它们索引行以获取其他行的方式吗?您可以使用dplyr::anti_join()实现这一点: 您也可以使用负索引执行此操作: library(dplyr) df <- data.frame(x = c(1,2,3,4), y = c(5,6,7,8)) df df$index <- 1:nrow(df) df df_s <- samp

在r中,如果我随机获取数据表中的一半行,那么如何获取另一半行。我一直在使用sample_n()首先抓取随机行,这是它们索引行以获取其他行的方式吗?

您可以使用dplyr::anti_join()实现这一点:


您也可以使用负索引执行此操作:

library(dplyr)
df <- data.frame(x = c(1,2,3,4),
                 y = c(5,6,7,8))
df
df$index <- 1:nrow(df)
df
df_s <- sample_n(df,2) 
df_s
df[-df_s$index,]
库(dplyr)

非常感谢much@devan如果这对你有效,那么你可以检查答案,表明它解决了你的问题。很高兴我能帮忙:)
> data
  c.1..5..6..7..10. c.3..4..5..1..9.
1                 1                3
2                 5                4
3                 6                5
4                 7                1
5                10                9
> sample
  c.1..5..6..7..10. c.3..4..5..1..9.
1                10                9
2                 7                1
3                 1                3
> remaining
  c.1..5..6..7..10. c.3..4..5..1..9.
1                 5                4
2                 6                5
> 
library(dplyr)
df <- data.frame(x = c(1,2,3,4),
                 y = c(5,6,7,8))
df
df$index <- 1:nrow(df)
df
df_s <- sample_n(df,2) 
df_s
df[-df_s$index,]