Preserve 'rownames' after use of family functions apply

2

I use the apply functions, and I need to have rownames left in the database after executing them. Consider:

dataframe=structure(list(Composição100g = structure(c("Valor energético (KJ)", 
"Proteína", "Glicídios", "Açúcar", "Amido", "Gorduras totais", 
"Gorduras saturadas", "Colesterol", "Fibras", "Sódio", "Ferro", 
"Ácido fólico", "Vitamina D", "Vitamina B12", "Vitamina C"), format.spss = "A22", display_width = 17L), 
    AllBran = structure(c(1150, 13, 46, 17, 29, 4.5, 0.9, 0, 
    28, 0.8, 8.8, 250, 3.1, 1, 0), format.spss = "F8.1"), Crunch = structure(c(1600, 
    6, 83, 39, 44, 2.5, 1, 0, 3, 0.7, 7.9, 333, 0, 0.8, 0), format.spss = "F8.1"), 
    CornFlakes = structure(c(1600, 7, 84, 7, 77, 0.8, 0.2, 0, 
    2.5, 0.9, 7.9, 167, 4.2, 0.8, 0), format.spss = "F8.1"), 
    ChocoKrispis = structure(c(1600, 6, 85, 35, 50, 2, 0.5, 0, 
    2, 0.6, 7.9, 333, 0, 0.8, 0), format.spss = "F8.1"), Muesli = structure(c(1150, 
    10, 58, 15, 43, 10, 2.5, 0, 9, 0.1, 5, 2, 3, 1, 0), format.spss = "F8.1"), 
    SpecialK = structure(c(1600, 16, 75, 17, 58, 1, 0.3, 0, 2.5, 
    0.8, 23.3, 333, 8.3, 1.7, 100), format.spss = "F8.1"), FrootLoop = structure(c(1550, 
    14, 74, 22, 52, 1, 0.3, 0, 3.5, 0.7, 21, 300, 7.5, 1.5, 90
    ), format.spss = "F8.1")), .Names = c("Composição100g", 
"AllBran", "Crunch", "CornFlakes", "ChocoKrispis", "Muesli", 
"SpecialK", "FrootLoop"), row.names = c(NA, -15L), class = "data.frame")

Where I executed:

#inserir uma variável comum como rownames
row.names(cereais)<-cereais$Composição100g
cereais[1]<-NULL

and after:

cereais<-data.frame(cereais<-apply(cereais,2,as.numeric))

and rownames is reset. I tried some functions found in SO , but when it does not give an error, it creates a list (which I do not want).

What do I do to preserve rownames within the dataframe after using apply functions?

    
asked by anonymous 15.09.2018 / 19:57

1 answer

3

The problem with your code, and losing the names attribute is that it is complicating what is much simpler.

At first connect, data.frame() is a function and therefore to assign a value to a function argument must use = and not <- . It would look like this:

data.frame(cereais = apply(cereais,2,as.numeric)))

Secondly, it is not necessary to assign a name, cereais to the result of apply within the data.frame() function. Just coerce the result of apply , an object of class matrix , into an object of class data.frame . This is done with the as.data.frame function.

cereais2 <- as.data.frame(apply(cereais, 2, as.numeric), row.names = row.names(cereais))

Now that's okay.

However, I would still like to say that the lapply and sapply functions are better in this case. They apply to objects of class list and as data.frame 's are lists make the code even simpler. It is not necessary to pass the dimension, the sapply will process each column vector, the members of the list.

cereais3 <- as.data.frame(sapply(cereais, as.numeric), row.names = row.names(cereais))

The results are identical.

identical(cereais2, cereais3)
#[1] TRUE
    
15.09.2018 / 20:50