R histogram that sums rather than frequency

自作多情 提交于 2019-12-24 13:00:11

问题


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.

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) #only shows frequency. How do i get it to sum mydata$BALANCE

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


回答1:


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

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)

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)

Then plot that using barplot:

barplot(plotdata)




回答2:


That's not an example that we can test to see if it meets your expectations, (and it's also not exactly clear what you do mean by "taking a sum parameter"), but try this:

hist( cumsum( mydata$BALANCE) )


来源:https://stackoverflow.com/questions/14741443/r-histogram-that-sums-rather-than-frequency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!