Q:

how to keep rows based on first digit of elements in r ?

I have the following data.frame
crime<-c(71040,142320,71013,71013,72113)
coded.month<-c("2018-10","2018-10","2018-10","2018-10","2018-10")
df<-data.frame(coded.month,crime)
  coded.month  crime
1     2018-10  71040
2     2018-10 142320
3     2018-10  71013
4     2018-10  71013
5     2018-10  72113

Bascially, I want to isolate all the rows where the first digit of crime is 7 so that I get the following
  coded.month  crime
1     2018-10  71040
3     2018-10  71013
4     2018-10  71013
5     2018-10  72113

how do I go about this?

A:

You may use substr:
df[substr(df$crime, 0, 1) == 7, ]
#   coded.month crime
# 1     2018-10 71040
# 3     2018-10 71013
# 4     2018-10 71013
# 5     2018-10 72113

