Create a data frame by separating terms from a string into columns

Asked

Viewed 55 times

0

How do I create a data.frame separating each term bound by _ in columns

"WZ_311205_20131007_20131008113131_RUA_RAMAL_FARIA_TEMP_DATA_ _AVENIDA_DADOS_PORTO_9.xml"
"WZ_311205_20131007_20131008113131_RUA_RAMAL_FARIA_TEMP_DATA_ _AVENIDA_DADOS_PORTO_10.xml"

1 answer

2


To do this, you can use the function stringr::str_split_fixed:

df <- structure(list(V1 = c("WZ_311205_20131007_20131008113131_RUA_RAMAL_FARIA_TEMP_DATA_ _AVENIDA_DADOS_PORTO_9.xml", 
"WZ_311205_20131007_20131008113131_RUA_RAMAL_FARIA_TEMP_DATA_ _AVENIDA_DADOS_PORTO_10.xml"
)), class = "data.frame", row.names = c(NA, -2L)) # Leitura da base de dados

ncols <- max(apply(df, 1, function(x) str_count(x, "_")))+1 # Número de colunas a serem criadas + 1

stringr::str_split_fixed(df$V1, "_", n=ncols)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.