-2

I have data table organized into columns "color" and "direction". In the "color" column, there are only 2 different variables, red and white.

I'm super new with working with R and was wondering how to split this data table into 2 separate data tables, one with only the data associated with red and another set with data only associated with white.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • This will give you subset of red colour: `myRed <- mydata[ mydata$color == "red", ]` – zx8754 Jun 09 '16 at 18:58
  • Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Jun 09 '16 at 18:58

1 Answers1

1

If you want match "red" exactly, you can use

myRed <- mydata[ mydata$color == "red", ] 
mywhite <- mydata[ mydata$color == "white", ] 

or, if you want to partially match, you can use ?grepl which returns a logical index for subsetting:

myRed = mydata[ grepl("red", tolower(mydata$color)),]
mywhite = mydata[ !grepl("red", tolower(mydata$color)),]
talat
  • 68,970
  • 21
  • 126
  • 157
Praveen DA
  • 358
  • 4
  • 17