converting output of R's “by” command to data frame

前端 未结 3 1598
抹茶落季
抹茶落季 2021-01-18 05:42

I\'m trying to use R\'s by command to get column means for subsets of a data frame. For example, consider this data frame:

> z = data.frame(         


        
3条回答
  •  长情又很酷
    2021-01-18 06:25

    Dealing with the by output can be really annoying. I just found a way to withdraw what you want in a format of a data frame and you won't need extra packages.

    So, if you do this:

    aux <- by(z[,2:5],z$labels,colMeans)
    

    You can then transform it in a data frame by doing this:

      aux_df <- as.data.frame(t(aux[seq(nrow(aux)),seq(ncol(aux))]))
    

    I'm just getting all the rows and columns from aux, transposing it and using as.data.frame.

    I hope that helps.

提交回复
热议问题