Question

I am trying to use a random list of sample IDs generated in R as the basis to subset a larger dataframe. I realize that I can just randomly subset directly but I'd like to know in case next time I need to subset using specific sample names.

I have data (mydata) that looks like this:

Species.Obj.ID  Sample  Sample.Name
1                23      George
2                90      Adilade
3                34      Frank
4                23      Steve

I generated random numbers

rand<-sample(1:4, 2, replace=F)

Then tried to subset the data using the example from here (How to filter a table's row based on an external list?).

subset(mydata,subset= Species.Obj.ID %in% rand)

But I just get back the full original data (mydata) in return.

I'd like to get the rows in the datatable from the random numbers in rand.

I'd greatly appreciate any advice on how to correct this. Thanks!

Was it helpful?

Solution

This way is a bit easier than using subset

> yourData <- data.frame(Species.Obj.ID = 1:4, 
                         Sample = c(23,90,34,23), 
                         Sample.Name = c("George", "Adilade", "Frank", "Steve"))

> rand <- sample(1:4, 2, replace = FALSE)

> rand
[1] 2 3  # the random numbers are 2 and 3

> yourData[rand, ]  # the rows shown are rows 2 and 3

  Species.Obj.ID Sample Sample.Name
2              2     90     Adilade
3              3     34       Frank

You can even shorten this all by using

yourData[sample(1:4, 2, replace = FALSE), ]

This will return a random sample of 2 rows of your original data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top