I have a three-column dataset df1 with this data:
ID Age Games
1 36 10
2 36 15
3 36 20
4 36 30
1 37 40
2 37 50
3 37 35
4 37 45
Here is the dput for the df1 dataset:
structure(list(ID = c(1, 2, 3, 4, 1, 2, 3, 4), Age = c(36, 36,
36, 36, 37, 37, 37, 37), Games = c(10, 15, 20, 30, 40, 50, 35,
45)), class = "data.frame", row.names = c(NA, -8L))
I want the data to be appear the way shown in the table below where the games for each ID's Age 36 and Age 37 values are now in separate columns:
ID Age36 Age37
1 10 0
2 15 0
3 20 0
4 30 0
1 0 40
2 0 50
3 0 35
4 0 45
My goal is to create from the second table a line chart with two line graphs, one for the Age36 games for each ID and the other for the Age 37 games, using pivot_longer to prepare the data for input into ggplot2 (unless you can recommend a better way of doing that).
ggplotinstead of the seconddf1 %>% mutate(Age = factor(str_c('Age', Age))) %>% ggplot(aes(x = ID, y = Games, group = Age)) + geom_line()mutate(Age = factor(str_c('Age', Age)?factorcolumn with 'Age' as prefix. The packages used arelibrary(dplyr); library(stringr)