I'm hoping one of you can help me, with this probably easy question. The thing is, I have a longitudinal dataframe, that looks something like this:
id <- c('1','1','1','2','2','2','3','3','3')
year <- c(2012, 2013, 2014, 2012, 2013, 2014, 2012, 2013, 2014)
ue <- c(1, 0, 0, 1, 1, 0, 1, 1, 1)
mydata <- data.frame(id, year, ue)
The ue
variable indicates whether a respondent received an unemployment benefit in a given year.
- First I want to create a simple table which tells me how many are receiving a ue-benefit in the specific year. Something like this:
year ue
1 2012 3
2 2013 2
3 2014 1
- I want to present the following table in an appropriate plot - ex a histogram.
I hope some of you can help you.
Thanks in advance.
CodePudding user response:
You can use tidyverse
to get a summary of the data.
library(tidyverse)
output <- mydata %>%
group_by(year) %>%
summarise(ue = sum(ue))
Output
# A tibble: 3 × 2
year ue
<dbl> <dbl>
1 2012 3
2 2013 2
3 2014 1
If you want the output of that table, then you probably want to use geom_col
from ggplot2
.
ggplot(output)
geom_col(aes(x = year, y = ue))
theme_bw()
Output