求和而不是频率的R直方图 [英] R histogram that sums rather than frequency

查看:73
本文介绍了求和而不是频率的R直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用R的新手,我试图生成一个直方图,其中轴显示的是另一列的总和,而不仅仅是频率计数.

I am new at using R and I am trying to produce a histogram where the axis shows a sum of another column rather than just a frequency count.

示例
我有一个包含两列的矩阵:RATE和BALANCE.我想生成一个显示余额总和的直方图,而不仅仅是记录计数.

example
I have a matrix with two columns, RATE and BALANCE. I would like to produce a histogram that shows the sum of balance rather than just record count.

hist(mydata$RATE)#仅显示频率.我如何求和mydata$BALANCE

hist(mydata$RATE) #only shows frequency. How do i get it to sum mydata$BALANCE

我想生成一个对BALANCE列求和的直方图,而不仅仅是进行记录计数.类似于hist(mydata$RATE, mydata$BALANCE),但显然hist函数似乎没有采用sum参数

I would like to produce a histogram that sums the BALANCE column rather than just doing a record count. something like hist(mydata$RATE, mydata$BALANCE) but obviously the hist function doesn't appear to take a sum parameter

推荐答案

听起来您正在尝试绘制条形图.相应的功能barplot可能有帮助.

It sounds like you're trying to plot a bar plot. The corresponding function barplot might help.

首先,按照@DWin的建议,创建一些可重现的数据:

First, as suggested by @DWin, create some reproducible data:

set.seed(1) # Sets the starting seed for pseudo-random number generation
mydata <- data.frame(RATE = sample(LETTERS[1:5], 100, replace = TRUE),
  BALANCE = rpois(100, 15) * 10)

然后使用功能tapply创建摘要数据.这将计算BALANCE变量与每个RATE变量值的总和.

Then create the summary data using the function tapply. This will calculate the sum of your BALANCE variable over each value of your RATE variable.

plotdata <- tapply(mydata$BALANCE, mydata$RATE, FUN = sum)

然后使用barplot进行绘图:

barplot(plotdata)

这篇关于求和而不是频率的R直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆