Jeremy K.
Jeremy K.

Reputation: 1792

separate every character in a word

I have some data that looks like this (reproducible code at the bottom):

  group1 group2
1  ABBBA  ACCCD

and I'd like to split every letter into its own sequentially numbered column.

So:

library(tidyverse)

df <- data.frame(stringsAsFactors=FALSE,
         group1 = c("ABBBA"),
         group2 = c("ACCCD")
)

df %>% separate(group1, sep = c(1, 2), into = c("1", "2", "3"))

gets me started:

  1 2   3 group2
1 A B BBA  ACCCD

and I could type our the rest, but there must be a more elegant way to get what I want which is:

  1 2 3 4 5 6 7 8 9 10
1 A B B B A A C C C D

Upvotes: 0

Views: 44

Answers (1)

Nicolas2
Nicolas2

Reputation: 2210

Prehaps you can try this (assuming that we may paste group1 and group2 like this on every row)

df <- data.frame(stringsAsFactors=FALSE,
     group1 = c("ABBBA","ZY"),
     group2 = c("ACCCD","X")
)

df %>% mutate(id=row_number()
             ,x=str_split(paste0(group1,group2),"(?<=.)(?=.)")) %>% 
  unnest %>% 
  group_by(id) %>% 
  mutate(k=row_number()) %>%
  spread(k,x)
## A tibble: 2 x 13
## Groups:   id [2]
#  group1 group2    id `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`   `10` 
#  <chr>  <chr>  <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#1 ABBBA  ACCCD      1 A     B     B     B     A     A     C     C     C     D    
#2 ZY     X          2 Z     Y     X     <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>

Upvotes: 2

Related Questions