how to write contains function in R

Hi Team,

How to write if find or contains function in R, and then created new column as page.

Page :- https://community.rstudio.com/
Page :- https://community.pen.com/
Example:-
If page contains "rstudio.com/ THEN Page = "Studio";
if Page contains "pen.com/ THEN Page = "PEN";
else "blank";
run;

You can use the grepl function from base R or the str_detect function from the stringr package to return TRUE or FALSE whether a string contains another string. With that, a standard if and else structure should get you what you need.

Page = "https://community.rstudio.com/"

grepl("rstudio.com/", Page)
#> [1] TRUE


library(stringr)
#> Warning: package 'stringr' was built under R version 3.5.3
str_detect(Page, "rstudio.com/")
#> [1] TRUE

Created on 2019-11-05 by the reprex package (v0.3.0.9000)

1 Like