0

I have different quarters like

Quarter           GrossMargin
2009 Q1           17.60%

2009 Q1            17.80%

2010 Q2             18.50%

2011 Q1             21.60%

See like this i have big data set. I want to plot this in R. I used qplot(df$Quarter, df$GrossMargin, binwidth=.5)

It came dots at the data points of each occurance. Like in x-axis 2009 Q1 there are correspondingly two data points in Y-axis one at 17.6 and other at 17.8. But i want an average value for each quarter, like in x-axis 2009 Q1 should correspond to the single value in Y-axis (that is average of 17.6 and 17.8). If i try to do mean(GrossMargin), it gives mean of whole column, which is of no use. All my quarters(2009-Q1,Q2,Q3, 2010-Q1,Q2,Q3) are in one column and corresponding values of GrossMargin in other single column. How to do this in R

Quarter GrossMargin
2009 Q1 17.60%
2009 Q1 17.80%
2009 Q1 22.40%
2009 Q1 17.70%
2009 Q1 17.50%
2009 Q1 17.50%
2009 Q1 18.40%
2009 Q1 18.70%
2009 Q1 21.10%
2009 Q1 13.60%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 18.50%
2009 Q1 17.70%
2009 Q1 18.20%
2009 Q1 30.20%
2009 Q1 17.60%
2009 Q1 19.00%
2009 Q1 14.60%
2009 Q1 17.50%
2009 Q1 18.40%
2009 Q1 21.60%
2009 Q1 21.10%
2009 Q1 18.40%
2009 Q1 17.30%
2009 Q1 14.60%
2009 Q1 17.60%
2009 Q1 17.50%
2009 Q2 17.50%
2009 Q1 14.60%
2009 Q1 17.60%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 17.30%
2009 Q1 17.60%
2009 Q1 18.50%
2009 Q1 18.20%
2009 Q1 17.50%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 30.20%
2009 Q1 17.60%
2009 Q1 18.40%
2009 Q1 30.20%
2009 Q1 18.50%
2009 Q1 39.30%
2009 Q1 17.50%
2009 Q1 17.70%
2009 Q1 17.70%
2009 Q1 17.30%
2009 Q1 19.40%
2009 Q1 20.90%
2009 Q1 17.70%
2009 Q1 18.70%
2009 Q1 17.50%
2009 Q1 17.70%
2009 Q1 17.50%
2009 Q1 17.50%
2011 Q1 18.20%
2011 Q1 17.60%
2011 Q1 17.70%
2011 Q1 17.70%
2011 Q1 18.50%
2011 Q1 19.00%
2011 Q1 21.60%
2011 Q1 17.50%
2011 Q1 14.60%
2011 Q1 30.20%
2011 Q1 18.40%
2009 Q1 21.10%
2010 Q2 17.50%
2010 Q2 17.70%
2010 Q2 17.70%
2010 Q2 17.50%
2010 Q2 17.50%
2010 Q2 18.20%
2010 Q2 18.50%
2010 Q2 17.70%
2010 Q2 17.60%
2010 Q2 17.30%
2010 Q2 14.60%
2010 Q2 17.70%
2010 Q2 18.40%
2010 Q2 17.60%
2009 Q1 17.70%
2009 Q1 19.40%
2009 Q1 17.50%
2009 Q1 17.50%
2009 Q1 20.90%
2009 Q1 19.00%
2009 Q1 17.60%
2009 Q1 17.70%
2009 Q1 19.00%
2009 Q1 17.70%
2009 Q1 17.50%
2009 Q1 17.50%
2009 Q1 17.50%

the are other 5 columns which are Order.id customer id, date etc

timma
  • 223
  • 1
  • 5
  • 15

1 Answers1

0

First, transform the % strings to numeric values:

df[2] <- as.numeric(gsub("%", "", as.character(df[ , 2])))

Calculate avergae GrossMargin for each Quarter:

dat <- aggregate(GrossMargin ~ Quarter, df, mean)

Plot:

plot(as.factor(dat$Quarter), dat$GrossMargin)

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Maybe you should put this as answer to original [OP question](http://stackoverflow.com/questions/18396525/how-to-get-plot-of-average-value-from-r) because this question in exact copy. – Didzis Elferts Aug 23 '13 at 07:45
  • @DidzisElferts I wasn't aware of the original question. – Sven Hohenstein Aug 23 '13 at 07:45
  • @SvenHohenstein Your approach works wonders except when I try to use it inside a function. Say that ````plotting <- function(a) { dat <- aggregate(a ~ Quarter, df, mean) }```` and then ````plot(as.factor(dat$Quarter), dat$a)```` I get Error in model.frame.default(formula = a ~ Quarter., data = df). Different variable length. Any thoughts ? – lomper Apr 14 '20 at 15:14