1

Is it possible to filter rows based on 1 value in that row? I am doing some sport stats analysis and I have a column that contains the final outcome and score like so: W 112-101. PS I know the PlusMinus doesn't make sense I am just trying to show the format of my code.

I would like to just pull all of the wins out of the data set. I have tried to use indexing but I am having a hard time. Thanks!

            Date                Home                 Away              PlusMinus         Final
1   Mon, Oct 29, 2018   Golden State Warriors   Chicago Bulls             36           W 149-124
2   Wed, Feb 6, 2019    Milwaukee Bucks         Washington Wizards        13           W 148-129
3   Fri, Jan 4, 2019    Milwaukee Bucks         Atlanta Hawks             39           L 112-144

NickA
  • 433
  • 2
  • 10
  • Can you show the expected output. Perhaps `subset(df1, grepl('^W', Final))` – akrun Oct 04 '20 at 22:54
  • @akrun it would just be a new dataset only containing the rows in ```Final``` that are a win, or in this case, "W" – NickA Oct 04 '20 at 22:55
  • What have you tried so far? There are a lot of SO posts already on filtering data, and it might help to see your approach, even if that's just for folks to help point you to preexisting posts – camille Oct 04 '20 at 23:01

1 Answers1

1

We can use a substring pattern match with grepl to return a logical vector for subseting the rows

df2 <- subset(df1, grepl('^W', Final)) 

Or using filter

library(dplyr)
library(stringr)
df2 <- df1 %>%
           filter(str_detect(Final, '^W'))
akrun
  • 874,273
  • 37
  • 540
  • 662