Separar una columna en varias columnas, cuando son datos especiales

You are not getting an Error, you are getting a Warning (advertencia) and separate() is filling the missing values with NA. You can suppress the Warning with

separate(..., fill = "right")

For example:

DF <- data.frame(Vacuna = c("VACUNA",
"opv_2_1a4_tar",
"neumococo23_1_otraedad_tar_enf",
"sr_1_5a14_tar",
"hepatitisb_2_especial_tar_vih"))
library(tidyr)
DF |> separate("Vacuna", into = c("A","B","C","D","E"))
#> Warning: Expected 5 pieces. Missing pieces filled with `NA` in 3 rows [1, 2,
#> 4].
#>             A    B        C    D    E
#> 1      VACUNA <NA>     <NA> <NA> <NA>
#> 2         opv    2      1a4  tar <NA>
#> 3 neumococo23    1 otraedad  tar  enf
#> 4          sr    1     5a14  tar <NA>
#> 5  hepatitisb    2 especial  tar  vih
DF |> separate("Vacuna", into = c("A","B","C","D","E"), fill = "right")
#>             A    B        C    D    E
#> 1      VACUNA <NA>     <NA> <NA> <NA>
#> 2         opv    2      1a4  tar <NA>
#> 3 neumococo23    1 otraedad  tar  enf
#> 4          sr    1     5a14  tar <NA>
#> 5  hepatitisb    2 especial  tar  vih

Created on 2023-10-20 with reprex v2.0.2

1 Like