Posso dividir valores separados por caracteres em uma célula em linhas com tidyr::separate_longer_delim
. No entanto, existe uma maneira simples de adicionar uma coluna com um índice?
Eu tenho (exemplo de 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
E eu quero
#> # 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