Problem separating a column in tidyr

Hi, I Have a new question. With this dataframe

dataf <- data.frame(
  stringsAsFactors = FALSE,
               MUN = c("02006 ALCADOZO",
                       "02007 ALCALA DEL JUCAR","02008 ALCARAZ","02009 ALMANSA",
                       "02010 ALPERA","02011 AYNA","02012 BALAZOTE",
                       "02013 BALSA DE VES","02021 CASAS DE JUAN NUEZ","02022 CASAS DE LAZARO",
                       "02023 CASAS DE VES"),
               REG = c(22, 214, 182, 6.478, 198, 51, 589, 28, 171, 11, 39)
)

I'm trying to separate the "MUN" column in two ("COD" and "MUN"). I use:

dataf <- dataf %>% separate(MUN, into = c("COD", "MUN"), sep = " ", remove = TRUE, convert = FALSE)

But then the MUN column is cut off, I mean, get this (02007 | ALCALA) instead of (02007 | ALCALA DEL JUCAR), which is what I want.

Any idea? Thank you all

For this specific case, the argument "extra" in separate should get what you want. This controls what happens when there are too many pieces to separate.

dataf <- dataf %>% separate(MUN, into = c("COD", "MUN"), sep = " ", remove = TRUE, convert = FALSE, extra = "merge")

I hadn't realised how useful that argument was. Now it works perfectly.

Regards

This topic was automatically closed 7 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.