Vero Piedra
Vero Piedra

Reputation: 23

Separate numeric characters within a column in R

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

Answers (2)

jpdugo17
jpdugo17

Reputation: 7106

Another way of doing this is:

  1. Select the first two numbers of all elements in Age except the last one (65).

  2. Paste the first two with '-' and the last two.

  3. 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

akrun
akrun

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

  1. Extract the first two characters - substr(Age, 1, 2)
  2. Extract the next two - substr(Age, 3, 4)
  3. Then, paste them together with a - in between
  4. For those elements having only two characters, the second substr call returns "", thus we have - at the end of the string
  5. Replace the - 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

Related Questions