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
click here.
– neves
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..– Rui Barradas
opa... was bad, still learning to use the Rstudio and the stack
– vinifrm