rtidyr

Split character separated values into rows and add a column with index


I can split character separated values in a cell into rows with tidyr::separate_longer_delim. However, is there a simpley way to add a column with an index?

I have (example from https://tidyr.tidyverse.org/reference/separate_longer_delim.html)

library(tidyr)
df <- tibble(id = 1:4, x = c("x", "x y", "x y z", NA))
df %>% separate_longer_delim(x, delim = " ")
#> # A tibble: 7 × 2
#>      id x    
#>   <int> <chr>
#> 1     1 x    
#> 2     2 x    
#> 3     2 y    
#> 4     3 x    
#> 5     3 y    
#> 6     3 z    
#> 7     4 NA 

And I want

#> # A tibble: 7 × 2
#>      id x     index
#>   <int> <chr> <int>
#> 1     1 x     1
#> 2     2 x     1
#> 3     2 y     2
#> 4     3 x     1
#> 5     3 y     2
#> 6     3 z     3
#> 7     4 NA    1

Solution

  • Try mutate + row_number() by the group id

    library(tidyverse)
    
    tibble(id = 1:4, x = c("x", "x y", "x y z", NA)) %>%
      separate_longer_delim(x, delim = " ") %>%
      mutate(index = row_number(), .by = id) 
    
    # A tibble: 7 × 3
         id x     index
      <int> <chr> <int>
    1     1 x         1
    2     2 x         1
    3     2 y         2
    4     3 x         1
    5     3 y         2
    6     3 z         3
    7     4 NA        1