Extract the first 2 Characters in a string

RSubstr

R Problem Overview


I need to extract the 1st 2 characters in a string to later create bin plot distribution. vector:

x <- c("75 to 79", "80 to 84", "85 to 89") 

I have gotten this far:

substrRight <- function(x, n){
  substr(x, nchar(x)-n, nchar(x))
}

invoke function

substrRight(x, 1)

Response

[1] "79" "84" "89"

Need to prints the last 2 characters not the first.

[1] "75" "80" "85"

R Solutions


Solution 1 - R

You can just use the substr function directly to take the first two characters of each string:

x <- c("75 to 79", "80 to 84", "85 to 89")
substr(x, start = 1, stop = 2)
# [1] "75" "80" "85"

You could also write a simple function to do a "reverse" substring, giving the 'start' and 'stop' values assuming the index begins at the end of the string:

revSubstr <- function(x, start, stop) {
  x <- strsplit(x, "")
  sapply(x, 
         function(x) paste(rev(rev(x)[start:stop]), collapse = ""), 
         USE.NAMES = FALSE)
}
revSubstr(x, start = 1, stop = 2)
# [1] "79" "84" "89" 

Solution 2 - R

Here's a stringr solution:

stringr::str_extract(x, "^.{2}")

Returns first 2 characters of x

Solution 3 - R

Use gsub...

x <- c("75 to 79", "80 to 84", "85 to 89") 

gsub(" .*$", "", x) # Replace the rest of the string after 1st space with  nothing
[1] "75" "80" "85"

Solution 4 - R

Similar to @user5249203, but extracts a number/group, instead of removing everything after the space. In this case, the values can be any number of consecutive digits.

x <- c("75 to 79", "80 to 84", "85 to 89")

sub("^(\\d+) to \\d+"$, "\\1", x)
# [1] "75" "80" "85"

If you wanted to extract the lower & upper bound in one call, rematch2 concise places each "named group" into its own tibble column.

rematch2::re_match(x, "^(?<lower>\\d+) to (?<upper>\\d+)$")
# # A tibble: 3 x 4
#   lower upper .text    .match  
#   <chr> <chr> <chr>    <chr>   
# 1 75    79    75 to 79 75 to 79
# 2 80    84    80 to 84 80 to 84
# 3 85    89    85 to 89 85 to 89

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSebView Question on Stackoverflow
Solution 1 - RdayneView Answer on Stackoverflow
Solution 2 - RBen GView Answer on Stackoverflow
Solution 3 - Ruser5249203View Answer on Stackoverflow
Solution 4 - RwibeasleyView Answer on Stackoverflow