Back
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?
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 themas.numeric(gsub("([0-9]+).*$", "\\1", years))[1] 20 1
# to find a set of numbers in the start and capture them
as.numeric(gsub("([0-9]+).*$", "\\1", years))
[1] 20 1
OR
# pattern is to just remove _years_oldas.numeric(gsub(" years old", "", years))[1] 20 1
# pattern is to just remove _years_old
as.numeric(gsub(" years old", "", years))
You can also use the strsplit function as follows:
#to split by space, get the element in first indexas.numeric(sapply(strsplit(years, " "), "[[", 1))[1] 20 1
#to split by space, get the element in first index
as.numeric(sapply(strsplit(years, " "), "[[", 1))
31k questions
32.8k answers
501 comments
693 users