我正在分析学生的测试数据,但在此之前我想对其进行整理。我试图建立一个整洁的数据框,但似乎行正在重复。下面是我的代码。
library(tidyverse)
(Test <- tibble(
Student = c("A", "B", "C", "D", "E"),
Test1 = c("SAT", "SAT", "SAT", "SAT", "SAT"),
Test2 = c("NA", "ACT", "ACT", "ACT", "ACT"),
testdate1 = c("7/1/2017", "6/1/2017", "3/1/2017", "2/17/2018", "NA"),
testdate2 = c("NA", "NA", "1/1/2016", "12/1/2016", "10/1/2016")
))
(Testa <- tibble(
Student = c("A", "B", "C", "D", "E"),
Test1 = c("SAT", "SAT", "SAT", "SAT", "SAT"),
Test2 = c("NA", "ACT", "ACT", "ACT", "ACT")
))
(Testb <- tibble(
Student = c("A", "B", "C", "D", "E"),
testdate1 = c("7/1/2017", "6/1/2017", "3/1/2017", "2/17/2018", "NA"),
testdate2 = c("NA", "NA", "1/1/2016", "12/1/2016", "10/1/2016")
))
(td1 <- Testa %>%
gather(Test1, Test2, key = "Test", value = "Score"))
(td2 <- Testb %>%
gather(testdate1, testdate2, key = "Dated", value = "Datev"))
(tidy <- left_join(td1, td2))
任何人都可以帮助我解决此问题。下面是我要如何查看数据的图像。
答案 0 :(得分:0)
您需要学生的ID和考试的ID
看看这是否有帮助
td1 <- Testa %>%
gather(Test1, Test2, key = "Test", value = "Score")
td2 <- Testb %>%
gather(testdate1, testdate2, key = "Dated", value = "Datev") %>%
mutate(Test=ifelse(Dated %in% "testdate1", "Test1", "Test2"))
tidy <- left_join(td1, td2)
tidy
答案 1 :(得分:0)
我认为最简单的解决方案是使用stats::reshape
,它能够收集多个列,如对此问题的回答所建议的:
Reshaping multiple sets of measurement columns (wide format) into single columns (long format)
stats:reshape
的接口不如整洁的接口漂亮,但是它可以通过一个函数调用来完成工作。