Preserve rownames after use of apply family functions

Asked

Viewed 58 times

2

I make use of the functions apply and, I need you to rownames stay in the database after execution of these. 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 ONLY, but when there is no error, a list is created (what I do not want).

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

1 answer

3


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

First call, data.frame() is a function and therefore to assign a value to a function argument must use = and not <-. Would that be:

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

Secondly, there is no need to name, cereais to the result of apply within the function data.frame(). Just coerce the result of apply, a class object matrix, in a class object data.frame. This is done with the function as.data.frame.

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

Now it’s all right.

However, I would also like to say that lapply and sapply are better in this case. Apply to class objects list and how the data.frame's are lists make the code even simpler. It is not necessary to pass the dimension, the sapply will process each array column, the list members.

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

The results are identical.

identical(cereais2, cereais3)
#[1] TRUE
  • I understood the concept you passed, @Rui Barradas. However, my goal is to keep rownames, that is, the column that names the rows of the dataframe. When I run my (and your) function, their names are replaced by numbers (1,2,3...).

  • 1

    @Giovani Sorry, I read badly! I’ll see better...

  • 1

    @Giovani Feito, look now.

  • All right, @Rui Barradas. Grateful.

Browser other questions tagged

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