Reputation: 23
in r I have a column that shows age ranges like this:
Age <- c(1524, 2534, 3544, 4554, 5564, 65)
So, as in ages from 15 to 24, 15-34 ... and older than 65 years
I want the ages in my column to be separated by a "-". And the last age to say 65<". And look like:
Age <- c(15-24, 25-34, 35-44, 45-54, 55-64, 65<)
Thanks
Upvotes: 1
Views: 42
Reputation: 7106
Another way of doing this is:
Select the first two numbers of all elements in Age
except the last one (65).
Paste the first two with '-' and the last two.
Append '65<' at the end.
library(tidyverse)
Age <- c(1524, 2534, 3544, 4554, 5564, 65)
str_sub(Age[-length(Age)], 1, 2) %>%
str_c('-', str_sub(Age[-length(Age)], -2, -1)) %>%
c(str_c(Age[length(Age)], '<'))
#> [1] "15-24" "25-34" "35-44" "45-54" "55-64" "65<"
Created on 2021-07-04 by the reprex package (v2.0.0)
Upvotes: 1
Reputation: 887168
We can use sub
to capture the first two characters ((..)
) from the start (^
) followed by the next two characters, insert the -
between the backreferences of the captured group in replacement. Then use ifelse
to paste
the element that doesn't have a -
with <
Age <- sub("^(..)(..)$", "\\1-\\2", Age)
ifelse(grepl("-", Age), Age, paste0(Age, "<"))
#[1] "15-24" "25-34" "35-44" "45-54" "55-64" "65<"
Or may do this in a single step with substr
and sub
substr(Age, 1, 2)
substr(Age, 3, 4)
paste
them together with a -
in betweensubstr
call returns ""
, thus we have -
at the end of the string-
at the end ($
- regex metacharacter for end of string) with <
sub("-$", "<", paste0(substr(Age, 1, 2), "-", substr(Age, 3, 4)))
[1] "15-24" "25-34" "35-44" "45-54" "55-64" "65<"
Upvotes: 3