I am interested in merging 3 datasets using the Pivot_Longer function.
The data I have is as follows:
A_1930 <- data.frame(grade = c("A","B","C","D"), total = c(0.5,0.35,0.7,0.45), white = c(0.6, 0.2, 0.1, 0.3), black = c(0.4,0.25,0.6,0.35), imm = c(0.2, 0.3, 0.6, 0.5), year = c(1930,1930,1930,1930))
B_1990 <- data.frame(grade = c("A","B","C","D"), total = c(0.45,0.5,0.7,0.5), white = c(0.65, 0.3, 0.15, 0.35), black = c(0.4,0.2,0.55,0.5), imm = c(0.25, 0.4, 0.5, 0.6), year = c(1990,1990,1990,1990))
C_2020 <- data.frame(grade = c("A","B","C","D"), total = c(0.45,0.3,0.6,0.5), white = c(0.5, 0.4, 0.1, 0.25), black = c(0.45,0.5,0.35,0.5), imm = c(0.25, 0.3, 0.55, 0.25), year = c(2020,2020,2020,2020))
I am interested in merging the datasets into something that looks like the following: The final table would include all three datasets.
YEAR | GRADE | NAME | VALUE |
---|---|---|---|
1930 | A | total | 0.45 |
1930 | B | white | 0.5 |
1930 | C | black | 0.45 |
Next, I am interested in creating a graph that looks like the following: Graph Expectation. Though, I am open to alternative visualizations if they make more sense. Thanks for your help with this!
library(dplyr)
library(tidyr)
bind_rows(A_1930, B_1990, C_2020) %>%
pivot_longer(-c(year, grade))
# # A tibble: 48 × 4
# grade year name value
# <chr> <dbl> <chr> <dbl>
# 1 A 1930 total 0.5
# 2 A 1930 white 0.6
# 3 A 1930 black 0.4
# 4 A 1930 imm 0.2
# 5 B 1930 total 0.35
# 6 B 1930 white 0.2
# 7 B 1930 black 0.25
# 8 B 1930 imm 0.3
# 9 C 1930 total 0.7
# 10 C 1930 white 0.1
# # ℹ 38 more rows
# # ℹ Use `print(n = ...)` to see more rows