7

I'm now trying to figure out a way to select data having specific values in a variable, or specific letters, especially using similar algorithm that starts_with() does.

Say I have a data named "school" as below:

Name Math English
James 80   90
Tom   91   91
Shaun 99   71
Jack  92   91

here, select(school, starts_with("M")) gives me the column "Math" only. I want to apply this on rows, such as to command 'give me the rows where name starts with "J" letter', which will in turn give me a data frame with two rows.

I tried transposing the data and succeeded at what I wanted to get, but it's not what I really want though.

How can get this to work?

micstr
  • 5,080
  • 8
  • 48
  • 76
Mons2us
  • 192
  • 1
  • 1
  • 9
  • 4
    You need `filter`, not `select` - `filter(school,substr(Name,1,1)=="J")` – Andrew Gustar Apr 05 '17 at 09:29
  • 1
    filter(grepl("^J", Name)) – jess Apr 05 '17 at 09:29
  • or `df[grepl("^J", df$Name), ]` – Ronak Shah Apr 05 '17 at 09:30
  • 4
    Possible duplicate of [Selecting rows in data.frame based on character strings](http://stackoverflow.com/questions/10067704/selecting-rows-in-data-frame-based-on-character-strings) Or [Get all the rows with rownames starting with ABC111](http://stackoverflow.com/questions/20825689/get-all-the-rows-with-rownames-starting-with-abc111) – Ronak Shah Apr 05 '17 at 09:31
  • Thank you all! I thought filter was not gonna work itout but it does! – Mons2us Apr 06 '17 at 08:53

2 Answers2

20

I think this will work:

library(tidyverse)
df <- df %>% filter(str_detect(Name, "^J"))
pjperez
  • 425
  • 4
  • 9
  • For those with big datasets and looking at grepl @Keiku's [benchmarking of grepl and str_detect](https://stackoverflow.com/a/40233929/4606130) shows str_detect is faster – micstr Jun 06 '22 at 12:41
  • @pjperez can you do data.table operation? – D. Shin Jan 24 '23 at 08:49
7

I believe that the combination of dplyr's filter and the substring command are the most efficient:

library(dplyr)
filtered_df <- school %>% dplyr::filter(substr(Name,1,1) == "J")
Vasilis Vasileiou
  • 507
  • 2
  • 8
  • 20