0 votes
1 view
in R Programming by (5.3k points)

I have a string like this:

years<-c("20 years old", "1 years old")

I would like to grep only the numeric number from this vector. The expected output is a vector:

c(20, 1)

How do I go about doing this?

1 Answer

0 votes
by (25.4k points)

To extract numbers from vectors of strings, you can use the gsub function from the base package as follows:

years<-c("20 years old", "1 years old")

# to find a set of numbers in the start and capture them

as.numeric(gsub("([0-9]+).*$", "\\1", years))

[1] 20  1


# pattern is to just remove _years_old

as.numeric(gsub(" years old", "", years))

[1] 20  1

You can also use the strsplit function as follows:

#to split by space, get the element in first index

as.numeric(sapply(strsplit(years, " "), "[[", 1))

[1] 20  1

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !