Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is that possible to get the index of unselected rows of data frame in R?

I want the indices of the unselected rows when using sample() in R. Consider the following case.

df <- data.frame(id = c(1,1,2,2,3,3),
                 v1 = c(2,2,9,4,7,1),
                 v2 = c(3,5,8,5,8,5))
ss  <- ceiling(0.5*nrow(df)) #size
set.seed(123)
rid <- sample(seq_len(nrow(df)),size=ss,replace=F)

Now, the rows 3,6,2 are randomly selected. Is there a way to know indices of unselected rows (1,4,5)?

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use df[-rid,]:

df <- data.frame(
  id = c(1, 1, 2, 2, 3, 3),
  v1 = c(2, 2, 9, 4, 7, 1),
  v2 = c(3, 5, 8, 5, 8, 5)
)
ss <- ceiling(0.5 * nrow(df)) # size
set.seed(123)
rid <- sample(seq_len(nrow(df)), size = ss, replace = F)

rid
#> [1] 3 6 2
df
#>   id v1 v2
#> 1  1  2  3
#> 2  1  2  5
#> 3  2  9  8
#> 4  2  4  5
#> 5  3  7  8
#> 6  3  1  5

df[rid,]
#>   id v1 v2
#> 3  2  9  8
#> 6  3  1  5
#> 2  1  2  5
df[-rid, ]
#>   id v1 v2
#> 1  1  2  3
#> 4  2  4  5
#> 5  3  7  8
rownames(df[-rid, ])
#> [1] "1" "4" "5"

Created on 2021-11-05 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading