简体   繁体   English

如何根据R中另一列的值总和绘制一列的因子?

[英]How to plot factors of one column against their total sum of values from another column in R?

I have a .csv file that I uploaded into R, it has about 2000 rows.我有一个上传到 R 中的 .csv 文件,它大约有 2000 行。 There is one column(causes) with 6 different causes, there is also another column(minutes), So each row has a cause and an amount of minutes.一列(原因)有 6 个不同的原因,还有另一列(分钟),所以每一行都有一个原因和分钟数。 I'd like to plot the different causes against their summed amount of minutes.我想根据它们的总分钟数绘制不同的原因。 I already figured out how to plot the amount of different factors in all rows together by doing:我已经弄清楚如何通过执行以下操作将所有行中不同因素的数量绘制在一起:

ggp <- ggplot(data.frame(table$cause_group), aes(x=table$cause_group)) +
  geom_bar()

Any help would be appreciated and sorry for the absolute beginner question.对于绝对的初学者问题,任何帮助将不胜感激和抱歉。

Cheers!干杯!

I would aggregate the data before plotting.我会在绘图之前汇总数据。

Base R:基础 R:

df <- aggregate(Minutes ~ Causes, sum, data = df)

you can also use dplyr for this (useful to get into dplyr when you are planning to work more with R in the future):您也可以为此使用 dplyr(当您计划在将来更多地使用 R 时进入 dplyr 很有用):

df <- df %>% group_by(Causes) %>% summarise_all(funs(sum))

Assuming the names of your columns are 'Causes' and 'Minutes'假设您的列的名称是“原因”和“分钟”

暂无
暂无

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

相关问题 R中另一列因子的标记因子 - Labeling Factors from another column of factors in R 如何根据一列中的值对数据进行装箱,并汇总R中另一列中的出现次数? - How to bin data based on values in one column, and sum occurrences from another column in R? 将一列的框 plot 与另一列的值显示为 x 轴 - Show box plot of one column against values from another column as x axis 如何根据R中的一列列表将一个数据框中的值汇总到另一个数据框中 - How to sum values from one data frame into another based on a column of lists in R 针对R中的一列绘制多列 - Plot Multiple columns against one column in R R-根据另一列中的类别从一列获取总和 - R - get sum from one column based on categories in another column 如何在一列中将值分成相等的范围,并在R中将另一列的关联值求和? - How do I split values into equal ranges in one column and sum the associated value of another column in R? R 中,如何向数据集添加一列,该数据集从一列中添加值并从另一列中减去值? - How to add a column to a dataset which adds values from one column and subtracts values from another column in R? 如何在与R中另一列中的重复项相关联的列中对值进行求和? - How to sum values in a column associated with duplicates in another column in R? R-如何将某一列中特定事件的总和添加到另一列中 - R - How to add the sum of a specific occurrence in one column to another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM