77

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"
zx8754
  • 52,746
  • 12
  • 114
  • 209
Seb
  • 979
  • 2
  • 7
  • 9

4 Answers4

117

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" 
dayne
  • 7,504
  • 6
  • 38
  • 56
37

Here's a stringr solution:

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

Returns first 2 characters of x

wake_wake
  • 1,332
  • 2
  • 19
  • 46
Ben G
  • 4,148
  • 2
  • 22
  • 42
6

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"
user5249203
  • 4,436
  • 1
  • 19
  • 45
1

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
wibeasley
  • 5,000
  • 3
  • 34
  • 62