tests/testthat/test-17-merge.R

# merge.edsurvey.data.frame testing and merge.light.edsurvey.data.frame testing
require(testthat)

options(width = 500)
options(useFancyQuotes = FALSE)

context("merge testing edsurvey.data.frame")
test_that("merge testing edsurvey.data.frame", {
  sdf <- readNAEP(system.file("extdata/data", "M36NT2PM.dat", package = "NAEPprimer"))
  lsdf <- getData(sdf, c(all.vars(composite ~ dsex + b017451), "ROWID", "origwt", "jkunit", "repgrp1"), addAttributes = TRUE)

  newdf <- data.frame(ROWID = lsdf$ROWID, newVar = lfactor(ifelse(lsdf$b017451 < 3, 1, 2), levels = c(1, 2), labels = c("Once a week or less", "More than once a week")))

  tblREF <- structure(c(`Once a week or less` = 6984L, `More than once a week` = 9347L), dim = 2L, dimnames = structure(list(c(
    "Once a week or less",
    "More than once a week"
  )), names = ""), class = "table")

  # merge.edsurvey.data
  sdfT1 <- merge(sdf, newdf, by = "ROWID")
  expect_is(sdfT1, "edsurvey.data.frame")
  expect_equal(table(sdfT1$newVar), tblREF)

  # merge.light.edsurvey.data
  sdfT2 <- merge(lsdf, newdf, by = "ROWID")
  expect_is(sdfT2, "light.edsurvey.data.frame")
  expect_equal(table(sdfT2$newVar), tblREF)
})

Try the EdSurvey package in your browser

Any scripts or data that you put into this service are public.

EdSurvey documentation built on June 27, 2024, 5:10 p.m.