dimanche 14 mars 2021

How to get the rest of the rows after taking some rows randomly from a dataframe in R

I have 2 dataframe df_1 and df_2. Now I have to select some rows randomly from df_1 and then I will merge the rest of the rows (which not selected randomly) from df_1 with df_2.

I am using this code

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)

But, when I am trying to extract the rows not selected randomly. My code is not working. I am getting the same data as the df_1 means 20 rows (same dataset)

Actually, I have to make 3 test and 3 train datasets.

Reproducible data (only 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))



Aucun commentaire:

Enregistrer un commentaire