Changing the row name of a list in R using lapply from a list variable

Asked

Viewed 389 times

2

I have a list called dataList and I would like the name of the lines to be equal to a variable called CNPJ that is within that list. For that, I first applied:

dadosLista<-lapply(dadosLista, "rownames<-", dadosLista$CNPJ)

However, dataLista$CNPJ is considered an empty variable. Thus, I created the CNPJ list:

CNPJ<-lapply(dadosLista, `[`, 3)

And then:

dadosLista<-lapply(dadosLista, "rownames<-", CNPJ)

Error appeared:

Error in `row.names<-.data.frame`(`*tmp*`, value = value) : 
'row.names' com comprimento inválido

Must be some problem I’m having with understanding lapply.

1 answer

1


I solved this problem using a for command as follows:

for (i in 1:340){
rownames(dadosLista[[i]])<-dadosLista[[i]][,c(3)]
}

Where column 3 represents the desired values for row names.

Browser other questions tagged

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