그래서 병합해야하는 세 가지 데이터 세트가 있습니다. 여기에는 4 학년과 5 학년의 학교 데이터 및 읽기/수학 점수가 포함됩니다. 그 중 하나는 일부 변수에서 누락 된 부분이 많은 긴 형식의 데이터 집합 (예, 긴 형식의 데이터가 필요함)이고 나머지 두 가지는 와이드 형식의 전체 누락 데이터 이러한 모든 데이터 프레임에는 데이터베이스의 각 개인에 대해 고유 한 ID 번호가있는 열이 있습니다. school_lf
, school4
및 school5
을 : 여기 와이드 폼 데이터로 NAs가있는 NAs를 오버라이드 할 수 있도록 긴 형식 데이터 병합
school_lf
는 NAS 및
school4
및
school5
와 긴 형식의 데이터가 나는 넓은 형태의 데이터를 병합 할 필요가
set.seed(890)
school <- NULL
school$id <-sample(102938:999999, 100)
school$selected <-sample(0:1, 100, replace = T)
school$math4 <- sample(400:500, 100)
school$math5 <- sample(400:500, 100)
school$read4 <- sample(400:500, 100)
school$read5 <- sample(400:500, 100)
school <- as.data.frame(school)
# Delete observations at random from the school df
indm4 <- which(school$math4 %in% sample(school$math4, 25))
school$math4[indm4] <- NA
indm5 <- which(school$math5 %in% sample(school$math5, 50))
school$math5[indm5] <- NA
indr4 <- which(school$read4 %in% sample(school$read4, 70))
school$read4[indr4] <- NA
indr5 <- which(school$read5 %in% sample(school$read5, 81))
school$read5[indr5] <- NA
# Separate Read and Math
read <- as.data.frame(subset(school, select = -c(math4, math5)))
math <- as.data.frame(subset(school, select = -c(read4, read5)))
# Now turn this into long form data...
clr <- melt(read, id.vars = c("id", "selected"), variable.name = "variable", value.name = "readscore")
clm <- melt(math, id.vars = c("id", "selected"), value.name = "mathscore")
# Clean up the grades for each of these...
clr$grade <- ifelse(clr$variable == "read4", 4,
ifelse(clr$variable == "read5", 5, NA))
clm$grade <- ifelse(clm$variable == "math4", 4,
ifelse(clm$variable == "math5", 5, NA))
# Put all these in one df
school_lf <-cbind(clm, clr$readscore)
school_lf$readscore <- school_lf$`clr$readscore` # renames
school_lf$`clr$readscore` <- NULL # deletes
school_lf$variable <- NULL # deletes
###############
# Generate the 2 data frames with IDs that have the full data
set.seed(890)
school4 <- NULL
school4$id <-sample(102938:999999, 100)
school4$selected <-sample(0:1, 100, replace = T)
school4$math4 <- sample(400:500, 100)
school4$read4 <- sample(400:500, 100)
school4$grade <- 4
school4 <- as.data.frame(school4)
set.seed(890)
school5 <- NULL
school5$id <-sample(102938:999999, 100)
school5$selected <-sample(0:1, 100, replace = T)
school5$math5 <- sample(400:500, 100)
school5$read5 <- sample(400:500, 100)
school5$grade <- 5
school5 <- as.data.frame(school5)
(id
및 grade
에 의해) NA의이 긴 형식의 데이터를 채우는 데 사용하는 데 필요한 DFS는 긴 형식 데이터로 NAs를 실제 값으로 바꿉니다. 아래 코드를 시도했지만 NA가있는 곳에 읽기 점수와 수학 점수를 병합하는 대신 여러 열을 소개합니다. 6 개의 개별 열 (read.x
, , math.x
, math.y
, mathscore
및 readscore
) 대신에 읽기 점수가있는 열과 수학 점수가있는 열이 하나만 있으면됩니다.
sch <- merge(school_lf, school4, by = c("id", "grade", "selected"), all = T)
sch <- merge(sch, school5, by = c("id", "grade", "selected"), all = T)
도움을 주신 데 대해 감사드립니다. 나는 이것을 몇 시간 동안 풀려고 노력했지만 아무런 진전을 이루지 못했다. (그래서 나는 여기서 물어 보았다)
덕분에 그 기능이 깔끔하게 보인다!! 환영보다 더 많은,하지만 난 문제가 무엇을하는지 이해하는 데 ... 내가 샘플 데이터로 실행 시도하고 나에게 오류를 제공합니다 :'mutate_impl (.data, dots)의 오류 : 'math4'객체를 찾을 수 없습니다. ' – rowbust
그러면 다른 데이터 세트에서 실행하고있을 것입니다. math4가 함수를 적용하는 데이터 세트에없는 것입니다. 에. – Edwin