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 (108k 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.

Browse Categories

...