Word Analysis using R studio

I need to compare two string, for example i have data <- c('"Trafik kembali lancar di kedua - dua arah. Selamat memandu","Kemalangan motorsikal di Km 224.8 utara dari Simpang Ampat ke Pedas Linggi. Tiada laluan terhalang.").

i want to make a comparison that says
if there is a word Pedas in data return heavy traffic else return traffic clear

plis help me !!

To clarify, R is the language, and RStudio is just the IDE (see FAQ below for disambiguation). Since this question isn't about the IDE itself, I'm going to move it to General.

What you're describing sounds like a good use-case for grepl() or a similar string-matching function which:

search for matches to argument pattern within each element of a character vector

str_detect() from the stringr package does ~ the same thing.

The chapter on strings from R for Data Science might be worth looking at.


i have try to run this code but there some error

regexrp("Dari simpang ampat",data[1:5])

the error is 
could not find function "regexrp"

i try to find its library but i could not found it

Hi @akmalirham! The function you're looking for is regexpr() (r after p, not p after r). It's part of the base, so if you have the name right you oughtn't need a package to use it :slight_smile:

ouh thank you so much my mistake

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.