【问题标题】:How to get the rest of the rows after taking some rows randomly from a dataframe in R从R中的数据框中随机获取一些行后如何获取其余行
【发布时间】:2021-03-14 17:32:53
【问题描述】:

我有 2 个数据框 df_1df_2。现在我必须从df_1 中随机选择一些行,然后将rest of the rows (which not selected randomly) from df_1df_2 合并。

我正在使用此代码

set.seed(9999)
df_1 <- # the whole dataset
test_dataset1 <- sample_n(df_1, 10)
train_part_1 <- df_1[which(!df_1 %in% test_dataset1)] # Not working
train_1 <- rbind(df_2, train_part_1)

但是,当我尝试提取未随机选择的行时。我的代码不起作用。我得到的数据与df_1 表示20 rows (same dataset) 的数据相同

已编辑:实际上,我必须制作 3 test3 train 数据集。那么,如何使用seed 函数来获取相同的数据集以进行重现?

可重现的数据(仅 df_1):

structure(list(nodeA = structure(c(4L, 2L, 1L, 1L, 1L, 4L, 1L, 
                                   9L, 3L, 4L, 2L, 8L, 2L, 1L, 5L, 7L, 3L, 6L, 2L, 1L), .Label = c("ID00309", 
                                                                                                   "ID00361", "ID00541", "ID00570", "ID00615", "ID00696", "ID00762", 
                                                                                                   "ID01200", "ID05109"), class = "factor"), nodeB = structure(c(8L, 
                                                                                                                                                                 3L, 3L, 1L, 2L, 7L, 9L, 8L, 8L, 6L, 9L, 7L, 4L, 4L, 6L, 9L, 6L, 
                                                                                                                                                                 7L, 5L, 5L), .Label = c("ID00361", "ID00541", "ID00570", "ID00615", 
                                                                                                                                                                                         "ID00696", "ID01200", "ID05109", "ID11641", "ID11691"), class = "factor"), 
               scr = structure(20:1, .Label = c("1.85284606048794", "1.90444166064472", 
                                                "1.90762235378507", "1.94364188077133", "1.95883206119256", 
                                                "2.08440437841349", "2.26408172709962", "2.3223132020942", 
                                                "2.46120775935034", "2.49647215035727", "2.50432367561777", 
                                                "2.57541320006514", "2.65099330092281", "2.75209155741549", 
                                                "2.93717640337986", "2.99596628688011", "3.21209741517806", 
                                                "3.21997803385465", "3.48788394772132", "3.81389707587156"
               ), class = "factor")), class = "data.frame", row.names = c(NA, 
                                                                          -20L))

【问题讨论】:

    标签: r random dplyr random-seed


    【解决方案1】:

    使用随机行号获取您的样本,并使用 - 获取逆:

    df_1 <- structure(list(nodeA = structure(c(4L, 2L, 1L, 1L, 1L, 4L, 1L, 9L, 3L, 4L, 
                                             2L, 8L, 2L, 1L, 5L, 7L, 3L, 6L, 2L, 1L), 
                                           .Label = c("ID00309", "ID00361", "ID00541", 
                                                      "ID00570", "ID00615", "ID00696", 
                                                      "ID00762", "ID01200", "ID05109"), 
                                           class = "factor"), 
                         nodeB = structure(c(8L, 3L, 3L, 1L, 2L, 7L, 9L, 8L, 8L, 6L, 
                                             9L, 7L, 4L, 4L, 6L, 9L, 6L, 7L, 5L, 5L), 
                                           .Label = c("ID00361", "ID00541", "ID00570", 
                                                      "ID00615", "ID00696", "ID01200", 
                                                      "ID05109", "ID11641", "ID11691"), 
                                           class = "factor"), 
                         scr = structure(20:1, .Label = c("1.85284606048794", "1.90444166064472", 
                                                          "1.90762235378507", "1.94364188077133", 
                                                          "1.95883206119256", "2.08440437841349", 
                                                          "2.26408172709962", "2.3223132020942", 
                                                          "2.46120775935034", "2.49647215035727", 
                                                          "2.50432367561777", "2.57541320006514", 
                                                          "2.65099330092281", "2.75209155741549", 
                                                          "2.93717640337986", "2.99596628688011", 
                                                          "3.21209741517806", "3.21997803385465", 
                                                          "3.48788394772132", "3.81389707587156"
                         ), class = "factor")), 
                    class = "data.frame", row.names = c(NA, -20L))
    
    set.seed(9999)
    Selected <- sample.int(nrow(df_1), 10)
    # index selected the row; use [col,row] pattern to select rows
    test_dataset1 <- df_1[ Selected, ] 
    # use -index to remove rows
    train_part_1  <- df_1[-Selected, ] 
    
    test_dataset1
    #>      nodeA   nodeB              scr
    #> 6  ID00570 ID05109 2.93717640337986
    #> 9  ID00541 ID11641 2.57541320006514
    #> 19 ID00361 ID00696 1.90444166064472
    #> 3  ID00309 ID00570 3.21997803385465
    #> 10 ID00570 ID01200 2.50432367561777
    #> 2  ID00361 ID00570 3.48788394772132
    #> 20 ID00309 ID00696 1.85284606048794
    #> 8  ID05109 ID11641 2.65099330092281
    #> 12 ID01200 ID05109 2.46120775935034
    #> 18 ID00696 ID05109 1.90762235378507
    train_part_1
    #>      nodeA   nodeB              scr
    #> 1  ID00570 ID11641 3.81389707587156
    #> 4  ID00309 ID00361 3.21209741517806
    #> 5  ID00309 ID00541 2.99596628688011
    #> 7  ID00309 ID11691 2.75209155741549
    #> 11 ID00361 ID11691 2.49647215035727
    #> 13 ID00361 ID00615  2.3223132020942
    #> 14 ID00309 ID00615 2.26408172709962
    #> 15 ID00615 ID01200 2.08440437841349
    #> 16 ID00762 ID11691 1.95883206119256
    #> 17 ID00541 ID01200 1.94364188077133
    

    reprex package (v1.0.0) 于 2021-03-14 创建

    【讨论】:

    • 非常感谢。您的代码正在解决我的问题,例如selected and -selected,但我得到了selected 的不同值(当我多次运行时)。如何重现相同的数据集?
    • 对不起,我不明白你需要什么。设置种子应该一次又一次地给你相同的向量。而且 - 当然 - 你也可以确保 Selected 在每次运行时都不会被覆盖。但由于这些答案太简单了,它不可能是你所需要的。
    • 谢谢。例如,当我运行这个Selected &lt;- sample.int(nrow(df_1), 10) 时,我得到`6 9 19 3 10 2 20 8 12 18`,而在Selected_2 &lt;- sample.int(nrow(df_1), 10) 时,我得到18 3 4 10 6 14 19 11 20 8。这意味着,如果我想要Selected and Selected_2 的相同输出,那么我必须从一开始就运行程序,而不是覆盖Selected。对吗?
    • 这就是set.seed() 的用武之地。相同的种子,相同的随机数。
    猜你喜欢
    • 2020-02-23
    • 2017-04-26
    • 1970-01-01
    • 2016-04-14
    • 2014-04-27
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    相关资源
    最近更新 更多