Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)
edited by
I am struggling with a project and for each record, there is a comment column. In that column, it says how long an individual stayed in a particular location. Some comments state "2 nights in A, 2 nights in B." As of right now, I am only able to filter out the first number. All I want is to get both numbers out of the comment, how can I do that?

1 Answer

0 votes
by (107k points)

In R programming, you can use the grepexpr() along with regmatches() as argument:

x <- "2 nights in A, 2 nights in B."

y <- regmatches(x, gregexpr("\\b\\d+\\b", x))[[1]]

y

[1] "2" "2"

The above will generate a vector containing all numbers in each individual string input.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...