R, which sums, not frequency

I am new to using R and I am trying to create a histogram where the axis shows the sum of another bar, not just the frequency.

Example
I have a matrix with two columns, RATE and BALANCE. I would like to create a bar chart that shows the amount of the balance, not just the number of entries.

hist(mydata$RATE)

# only shows the frequency. How can I get the amountmydata$BALANCE

I would like to create a bar chart that summarizes the BALANCE column rather than just making a count of entries. something like hist(mydata$RATE, mydata$BALANCE)

, but obviously the hist function doesn't take a sum parameter

+1


source to share


2 answers


It looks like you are trying to build a bar. The corresponding function barplot

can 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 summary data using the function tapply

. This will calculate the sum of your variable BALANCE

over each value of your variable RATE

.



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

      

Then sketch using barplot

:

barplot(plotdata)

      

enter image description here

+2


source


This is not an example we can test to make sure it meets your expectations (nor is it clear what you mean by "taking the sum parameter"), but try this:



hist( cumsum( mydata$BALANCE) )

      

0


source







All Articles