1

I have a dataframe that returns the following table (for example)

  ID      AVG        SIG     
 5X1PK  0.2712   0.004780167 
 DF5TB  39.4279  59.3092     
 5X1PK  64.8847  0.3614      
 DJII2  59.6743  26.7279     
 DRQUW  43.8942  0.2261      
 DRQUU  44.0606  0.128       
 DRQUW  43.6278  0.1562      

I am able to get the rows for one value of a column using this formula

data2 <- data1[data1$ID == ('5X1PK'),]

Suppose, I want to get all rows for the ID's (5X1PK, DRQUW, DRQUU) then the desired output that I want is

  ID      AVG        SIG     
 5X1PK  0.2712   0.004780167 
 5X1PK  64.8847  0.3614      
 DRQUW  43.8942  0.2261      
 DRQUU  44.0606  0.128       
 DRQUW  43.6278  0.1562      

How do I get rows for multiple values of ID in one table?

User7598
  • 1,658
  • 1
  • 15
  • 28
Sharath
  • 2,225
  • 3
  • 24
  • 37
  • 4
    Please put your data in a more friendly [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) format. Also, give an example of your desired output. It's unclear to me exactly what you are trying to do. – MrFlick Mar 25 '15 at 20:42
  • @MrFlick Thanks for pointing it out, I edited it to be more clear on the desired output. – Sharath Mar 25 '15 at 20:54

2 Answers2

4

You can use %in%. For example,

data2 <- data1[data1$ID %in% c("5X1PK", "DRQUW", "DRQUU"), ]
data2
#      ID     AVG         SIG
# 1 5X1PK  0.2712 0.004780167
# 3 5X1PK 64.8847 0.361400000
# 5 DRQUW 43.8942 0.226100000
# 6 DRQUU 44.0606 0.128000000
# 7 DRQUW 43.6278 0.156200000
blakeoft
  • 2,370
  • 1
  • 14
  • 15
1

This also works...

data2 <- data1[data1$ID == '5X1PK',]

To select multiple IDs you can add to the code as shown below:

data2 <- data1[data1$ID == '5X1PK' | data1$ID =='DF5TB',]
User7598
  • 1,658
  • 1
  • 15
  • 28