Home > Mobile >  Is there a R function to select a specific word from a column that contains different sentences as c
Is there a R function to select a specific word from a column that contains different sentences as c

Time:04-28

How can I select a specific word from a variable column that contains different sentences as character strings? Column is like that:

Earthquake$place <- c("18 km SSE of Sunnyside, Utah",
                      "2 km E of Magna, Utah",
                      "19 km WSW of Fairview, Wyoming")

I want to select rows that contains only earthquake place "Magna,Utah"

CodePudding user response:

You will have to use regular expression for this.

library(tidyverse)
Earthquake %>%
   filter(str_detect(place, 'Magna, ?Utah'))
                  place
1 2 km E of Magna, Utah

Explanation:

Since your aim is Magna, Utah You will have to use grepl/str_detect with the pattern Magna, ?Utah where we are literally matching the words Magna and Utah and whether or not there is a space in between them. The question mark is used to denote eithe a presence or absence of the letter/character before it.

In Base R

subset(Earthquake, grepl('Magna, ?Utah', place))
                  place
2 2 km E of Magna, Utah

CodePudding user response:

Is this what you are looking for?

data:

place <- c("18 km SSE of Sunnyside, Utah",
                      "2 km E of Magna, Utah",
                      "19 km WSW of Fairview, Wyoming")


library(stringr) 
str_match(str_match(place, 'Magna, Utah')

Result

     [,1]
[1,] NA
[2,] "Magna, Utah"
[3,] NA
  •  Tags:  
  • r
  • Related