-1

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!

Kimberly
  • 5
  • 2
  • 1
    Stack Overflow is not a homework or work task tutoring site nor is it a tutorial site. That said, if you try to address the problem yourself and get stuck, many here are willing to help. Please [edit] your question with what you have tried so far and the specific issue you are having. See [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more. – Ian Campbell Apr 30 '23 at 17:44
  • 1
    Also, please clarify how this question is different than your previous question: ["Graphing Multiple Column Averages from Different dfs Representing Different Years"](https://stackoverflow.com/q/75241687/13095326) – Ian Campbell Apr 30 '23 at 17:45
  • Additionally, when providing sample data, it is really appreciated when it actually parses correctly. In this case, I get errors about `Error: object 'A' not found`, because I believe you intend `c("A","B",...)` instead. – r2evans Apr 30 '23 at 17:48

1 Answers1

1
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
r2evans
  • 141,215
  • 6
  • 77
  • 149