Separation of rows into columns in R

Asked

Viewed 865 times

1

Friends, I am learning R. I am selecting the biggest consumers of some types of beverage in South America. I need to separate the dataframe below in the following columns: pais, bebida and litros, with the appropriate line values (records). Ex: Pais = Venezuela, bebida = cerveja, litros = 333. Can help?

structure(list(key = c("pais", "litros_cerveja", "pais.1", 
"litros_spirit", 
"pais.2", "litros_vinho", "pais.3", "litros_alcool_puro"), value = 
c("Venezuela", 
"333", "Guyana", "302", "Argentina", "221", "Argentina", "8.3"
)), row.names = c(NA, -8L), class = "data.frame")
  • 3
  • Pictures are a bad way to post data or code. Can you please, edit the question with the departure of dput(dados) or, if the base is too large, dput(head(dados, 20))? Note: dados is the base name, for example a data frame..

  • opa... was bad, still learning to use the Rstudio and the stack

1 answer

2


A trick to select alternate lines is to use a logical vector in indexing, which will be recycled over the entire length of the date frame.:

dados.brutos <- structure(list(key = c("pais", "litros_cerveja", "pais.1", "litros_spirit", "pais.2", "litros_vinho", "pais.3", "litros_alcool_puro"), value = c("Venezuela", "333", "Guyana", "302", "Argentina", "221", "Argentina", "8.3" )), row.names = c(NA, -8L), class = "data.frame")

> dados.brutos[c(TRUE, FALSE), ]
     key     value
1   pais Venezuela
3 pais.1    Guyana
5 pais.2 Argentina
7 pais.3 Argentina

Separating data from even and odd rows and joining them as separate columns will get what you need:

dados.arrumados <- cbind(dados.brutos[c(TRUE, FALSE), 2], # 2 ou "value", para selecionar apenas a coluna com o nome dos países
                         dados.brutos[c(FALSE, TRUE), ] )

To finish, put the proper names for the variables and remove the "liters" at the beginning of the name of the drinks (I’m assuming that all the data are in liters, otherwise skip this part):

names(dados.arrumados) <- c("país", "bebida", "litros")
dados.arrumados$bebida <- gsub("^litros_", "", dados.arrumados$bebida)

> dados.arrumados
       país      bebida litros
2 Venezuela     cerveja    333
4    Guyana      spirit    302
6 Argentina       vinho    221
8 Argentina alcool_puro    8.3
  • Very show. Thank you

Browser other questions tagged

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