简体   繁体   English

如何在 R 中添加分类变量时合并多列

[英]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 Rstack

setNames(stack(df1)[2:1], c('plant', 'value'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多列的频率计数与链接到R中的摘要统计信息的分类变量的转置结合在一起 - How to combine a frequency count of multiple columns with a transposition of categorical variables linked to summary statistics in R 如何合并R中的多列 - How to combine multiple columns in R 如何获取按R中分类变量的值分层的列的统计信息 - How to get stats of columns stratified by values of a categorical variable in R 在一个虚拟变量中组合多个分类变量 - Combine multiple categorical variables in one dummy variable 在R中将分类列转换为多个二进制列 - Converting Categorical Columns into Multiple Binary Columns in R 如何在 R 中组合多个数据框列 - How to combine multiple data frame columns in R 如何将R中的多个种族列合并为一个? - How to combine multiple ethnicity columns into one in R? 如何使用 tidymodels 处理多列中的一个分类变量? - How to handle one categorical variable in multiple columns using tidymodels? 如何按多列组计算分类变量中唯一值的数量 - How to count number of unique values in a categorical variable, by groups of multiple columns 如何在 R 中可视化多个分类变量与一个连续变量 - How to visualize multiple categorical variables vs one continuous variable in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM