[英]How to combine multiple columns while adding categorical variable in R
I'm trying to build a bar plot, and currently my data looks like this:我正在尝试构建一个酒吧 plot,目前我的数据如下所示:
tree flower shrub 4 4 4 5 3 2 4 1 3树 花 灌木 4 4 4 5 3 2 4 1 3
I want the column names to be x-values, and the mean of each column to be represented by a separate bar.我希望列名是 x 值,并且每列的平均值由单独的条形表示。 I think the best way to get at this would be to change my data so it looks like this:我认为解决此问题的最佳方法是更改我的数据,使其看起来像这样:
plant value tree 4 tree 5 tree 4 flower 4 flower 3 flower 1 shrub 4 shrub 2 shrub 3植物价值 树 4 树 5 树 4 花 4 花 3 花 1 灌木 4 灌木 2 灌木 3
Does anyone know the best way to go about this?有谁知道 go 关于这个的最佳方法? Or have any other suggestions for building a bar plot based on this data?或者有什么其他建议可以根据这个数据建一个酒吧 plot?
We can use stack
from base R
我们可以使用来自base R
的stack
setNames(stack(df1)[2:1], c('plant', 'value'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.