Samuel Harper
Samuel Harper

Reputation: 399

Using R to group values within different vectors so they can be plotted (ggplot2)

I have a question about how to group different vectors from a dataframe, in order to compare and analyse them. For example using ggplot2 to plot some graphs. To make it clearer I will provide the type of dataframe I am working with.

 ID      Date    |X |Y |Z |   BR
---------------------------------
6001-102| 2016-03| 1| 1| 1|  1.0
--------------------------------
6001-102| 2016-03| 1| 1| 1|  1.0
--------------------------------
6001-102| 2016-03| 1| 1| 1|  1.0
--------------------------------
6044-460| 2016-03| 2| 1| 4|  0.5
---------------------------------

The data columns I am focused on here are Date, Z and BR.

The dates are characters containing the month and years, for example 2016-03 and 2015-05, whilst Z is numeric and ranges from 1-8. I am finding this complicated for myself, because what I want R to do is to first group the results by the date (for example looking at only May 2015) and then get the average BR for each level of Z. Z represents different time groups, so if I was using ggplot I would see the average BR for each time group in May.

Can anyone show me a good example or maybe a previous question that is trying to accomplish the same as me? Hopefully with ggplot2? I haven't found one, but I am sorry if this is a duplicate question.

Thank you for your help!

Edit: Removed dput as question answered.

Upvotes: 0

Views: 137

Answers (1)

Aetos
Aetos

Reputation: 50

You can use mydf %>% group_by(Date_fill, y) %>% summarise(z = sum(z, na.rm=TRUE))

Upvotes: 1

Related Questions