Creating new entries with 4 digits in a digit row

I have a dataframe with numeric entries. The entries all consist of numbers that are at least 4 or multiples of four. I would now like to separate the numbers so all of them consit of four digits and are separated by either a ',' or in a seperate column.

y<-c(17938289, 123458938, 780983483029)

x<-c(0,1,2)
df<-data.frame(x,y)
df
x y
1 0 17938289
2 1 12345893
3 2 780983483029

The entry for y1 should be like: 1793, 8289
The entry for the next row should be 1234, 5893 etc.

Unfortunately I have no clue how to fix this problem. I'd be grateful for some help!

y <- c(17938289, 123458938, 780983483029)

x <- c(0, 1, 2)
df <- data.frame(x, y)

library(tidyverse)
library(stringi)
df %>%
  rowwise() %>%
  mutate(
    nch = nchar(y),
    mults_of_4 = nch %/% 4,
    trunc_to_mults_of_4 = str_sub(y, end = mults_of_4 * 4),
    splits = list(stri_sub(trunc_to_mults_of_4, 
                           seq(1, mults_of_4 * 4, by = 4),
                           length = 4))
  ) %>%
  unnest(cols = splits) %>%
  group_by(x) %>%
  mutate(nr = row_number()) %>%
  pivot_wider(
    names_prefix = "str_",
    names_from = "nr",
    values_from = "splits"
  )

Here is an alternative I was working on while @nirgrahamuk was posting his answer.

y<-c(17938289, 12345893, 780983483029)
x<-c(0,1,2)
df<-data.frame(x,y)
df
#>   x            y
#> 1 0     17938289
#> 2 1     12345893
#> 3 2 780983483029
library(tidyr)
MaxNewCols <- max(nchar(df$y) %/% 4) 
ColNames <- LETTERS[1:MaxNewCols]
df <- separate(data = df, col = y, into = ColNames, 
         sep = seq(4, 4 * MaxNewCols - 1, by = 4), fill = "right")
df
#>   x    A    B    C
#> 1 0 1793 8289     
#> 2 1 1234 5893     
#> 3 2 7809 8348 3029

Created on 2022-04-22 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.