1

I am trying to use the subset function to pick out certain lines of a data frame which contain the symbols * or +. I would like to put these entire lines of my data frame into a new data frame. I think that subset will be the best way to do this.

Below is my attempt:

nba <- read.csv('nba.csv',header=FALSE)

nba
two <- grep('\\Q*\\E',nba$V2)
one <- grep('\\Q+\\E',nba$V2)

both <- c(one,two)

allstar <-  subset.data.frame(nba, both)

If anyone can give me advice thank you. I apologize if I have any formating mistakes. This is my first time on this website.

Thank you.

Note: I am working in R studio

Robert
  • 19
  • 2
  • I understand why it isn't working, but is there a way to make it work? sample code would be ideal. i.e. allstar<-subset.data.frame(nba,....) – Robert Oct 21 '16 at 21:54
  • Welcome to SO. First of all you should read [here](http://stackoverflow.com/help/how-to-ask) about how to ask a good question; a good question has better changes to be solved and you to receive help. On the other hand a read of [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is also good. It explains how to create a reproducible example in R. Help users to help you by providing a piece of your data a desired output and things you have tried so far. – SabDeM Oct 21 '16 at 22:10

1 Answers1

0

subset takes logical values for subset argument. Use grepl ("l" for "logical") instead:

two <- grepl('\\Q*\\E',nba$V2)
one <- grepl('\\Q+\\E',nba$V2)

both <- one | two  ## logical "or" operation

allstar <-  subset.data.frame(nba, both)

Note, two, one and both have length nrow(nba).

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248