Home > front end >  find out which country starts with certain character using R
find out which country starts with certain character using R

Time:05-11

i have a table country called gDat with column country, now i want to find out the country starts with Mal with this code

grep('Mal', levels(gDat$country))

but its returns only the id of that country (not the country name), how to solve this?

CodePudding user response:

Use grepl with the regex pattern ^Mal:

countries <- gDat$country[grepl("^Mal", gDat$country)]

CodePudding user response:

You can use startsWith

gDat$country[startsWith(gDat$country, "Mal")]

or grep with value = TRUE.

grep("^Mal", gDat$country, value = TRUE)
  •  Tags:  
  • r
  • Related