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?