0
I have a data.table with a column that has the code of the municipalities (6 first characters) and the name of the municipalities (other characters).
I would like to separate them, using data.table.
In data.frame, it would be like this:
pop_mun_total$cod_mun <- str_sub(pop_mun_total$mun,start=1, end=6)
This also works in data.table, but it has some specific function?
Example of data.table:
pop_mun_total <- data.table(mun=c("110001 Alta Floresta D'Oeste", "110037 Alto Alegre dos Parecis","110040 Alto Paraíso", "110034 Alvorada D'Oeste", "110002 Ariquemes","110045 Buritis", "110003 Cabixi", "110060 Cacaulândia", "110004 Cacoal","110070 Campo Novo de Rondônia", "110080 Candeias do Jamari","110090 Castanheiras", "110005 Cerejeiras", "110092 Chupinguaia","110006 Colorado do Oeste", "110007 Corumbiara", "110008 Costa Marques","110094 Cujubim", "110009 Espigão D'Oeste", "110100 Governador Jorge Teixeira"))
How I’d like you to stay:
cod_mun mun
110001 Alta Floresta D'Oeste
110037 Alto Alegre dos Parecis
... ...
Instead of replacing the first space with "_", you can use a regular expression that identifies the first space after digits:
tstrsplit(mun, "(?<=\\d) ", perl = TRUE)
– Carlos Eduardo Lagosta