Make the variables created with lapply be allocated in their respective dataframe within a list

Asked

Viewed 47 times

3

Use the function lapply and wish the results to return within their respective dataframe within the list. Consider the following function:

x<-lapply(list,function(x)paste(x$Sigla,x$Município,sep='')) # onde `Sigla` e `Município`são variáveis em comum em todos os dataframes

This returns me a list x only with the results (concatenated).

How to adjust this function to get what you want?

I can’t post the dput, because the list is too long.

1 answer

3


Using the following example:

lista <- list(
  A = data.frame(Sigla = sample(LETTERS, 20, rep = T), Município = sample(letters, 20, rep = T)),
  B = data.frame(Sigla = sample(LETTERS, 20, rep = T), Município = sample(letters, 20, rep = T)),
  C = data.frame(Sigla = sample(LETTERS, 20, rep = T), Município = sample(letters, 20, rep = T))
)

You can use to allocate the new variable within its respective dataframe with the command cbind:

lapply(lista, function(x)cbind(x, Variável = paste(x$Sigla,x$Município,sep='')))

Browser other questions tagged

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