Reputation: 1099
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"
Upvotes: 88
Views: 190790
Reputation: 18642
strtrim(x, 2)
# [1] "75" "80" "85"
Or
sprintf("%.2s", x)
# [1] "75" "80" "85"
Upvotes: 0
Reputation: 4338
Here's a stringr
solution:
stringr::str_extract(x, "^.{2}")
Returns first 2 characters of x
Upvotes: 43
Reputation: 5287
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
Upvotes: 0
Reputation: 7784
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"
Upvotes: 131
Reputation: 4648
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"
Upvotes: 5