context("SummarizeVariable")
x0 <- 1:10
x.logical <- c(TRUE, TRUE, FALSE, TRUE, TRUE, FALSE)
x.numeric <- x0/10
x.categoric <- factor(letters[c(1,1,1,2,3,4,5,6)], levels = letters[6:1])
tmp.labels <- sprintf("$%d,000", 1:6)
x.categoric.with.comma <- factor(tmp.labels[c(1,1,1,2,3,4,5,6)])
x.date <- as.Date("2010-04-01") + 1:10
x.qdate <- structure(c(1327795200, 1330041600, 1328486400, 1330214400, 1331596800,
1325376000, 1326412800, 1329609600, 1327017600, 1331942400, 1330300800,
1326067200, 1332201600, 1330387200, 1330905600, 1331942400, 1328659200,
1332460800, 1332028800, 1330560000, 1331510400, 1328400000, 1330387200,
1328918400, 1330992000, 1326499200, 1329350400, 1327017600, 1326585600,
1332806400, 1326844800, 1328140800, 1326844800, 1330646400, 1325635200,
1332892800, 1326499200, 1329177600, 1331424000, 1330387200, 1332633600,
1326240000, 1329782400, 1332115200, 1331856000, 1328400000, 1330819200,
1329782400, 1332374400, 1328745600, 1326067200, 1327708800, 1329091200,
1332806400, 1329436800, 1332460800, 1332892800, 1328054400, 1331251200,
1328745600, 1325894400, 1332547200, 1325462400, 1332374400, 1328486400,
1333065600, 1325635200, 1327708800, 1330646400, 1331337600, 1330732800,
1326412800, 1332288000, 1326412800, 1329868800, 1328400000, 1325376000,
1332201600, 1327622400, 1328400000, 1329091200, 1329868800, 1329436800,
1333065600, 1329955200, 1329523200, 1330732800, 1331337600, 1328659200,
1329436800, 1328227200, 1329523200, 1331424000, 1327622400, 1332806400,
1330041600, 1332979200, 1326758400, 1326067200, 1326412800, 1330387200,
1331856000, 1331856000, 1332806400, 1329955200, 1329091200, 1329091200,
1332028800, 1326240000, 1332547200, 1329523200, 1326412800, 1330560000,
1330819200, 1332979200, 1329436800, 1330387200, 1326585600, 1331769600,
1330560000, 1326758400, 1330905600, 1332288000, 1329523200, 1331078400,
1329436800, 1331942400, 1327449600, 1328400000, 1330646400, 1327276800,
1331424000, 1328486400, 1331424000, 1330041600, 1330128000, 1325548800,
1332028800, 1332979200, 1330905600, 1326585600, 1330041600, 1328400000,
1326931200, 1329350400, 1326499200, 1326240000, 1331251200, 1326240000,
1325808000, 1326412800, 1326844800, 1330128000, 1328659200, 1327363200,
1332374400, 1330732800, 1325721600, 1327017600, 1331856000, 1332979200,
1325894400, 1326672000, 1326758400, 1327881600, 1332547200, 1329436800,
1326672000, 1329782400, 1329868800, 1325548800, 1327363200, 1327276800,
1331769600, 1329523200, 1326067200, 1330387200, 1329004800, 1329091200,
1332201600, 1327104000, 1327622400, 1330646400, 1330387200, 1332806400,
1333065600, 1330819200, 1332806400, 1330905600, 1326499200, 1330128000,
1325376000, 1330560000, 1331596800, 1331942400, 1326758400, 1330560000,
1331164800, 1329436800, 1330387200, 1338076800, 1336608000, 1339200000,
1337126400, 1336089600, 1339718400, 1340928000, 1333584000, 1338163200,
1336953600, 1340841600, 1333929600, 1339804800, 1339372800, 1333238400,
1333756800, 1337904000, 1340064000, 1336176000, 1333843200, 1337299200,
1333411200, 1336262400, 1338940800, 1337040000, 1336089600, 1335398400,
1335225600, 1335052800, 1340236800, 1337472000, 1333238400, 1341014400,
1335398400, 1339200000, 1333497600, 1340150400, 1334793600, 1335571200,
1339027200, 1333411200, 1339113600, 1339459200, 1335052800, 1337212800,
1335398400, 1336608000, 1338076800, 1334620800, 1338595200, 1336780800,
1340150400, 1337904000, 1337990400, 1340150400, 1333584000, 1335916800,
1334188800, 1339545600, 1336521600, 1338854400, 1340064000, 1334707200,
1335312000, 1341014400, 1334448000, 1333584000, 1340755200, 1337558400,
1334275200, 1334620800, 1334620800, 1336608000, 1336694400, 1339977600,
1335744000, 1333497600, 1333929600, 1338595200, 1338940800, 1334707200,
1333324800, 1340236800, 1333670400, 1339718400, 1337385600, 1335484800,
1334620800, 1335398400, 1335657600, 1335312000, 1338076800, 1340755200,
1336348800, 1334534400, 1333324800, 1336262400, 1334448000, 1339718400,
1335225600, 1338854400, 1334448000, 1339891200, 1333670400, 1334966400,
1338163200, 1337040000, 1340928000, 1338249600, 1335484800, 1338940800,
1334880000, 1340496000, 1334016000, 1340323200, 1339372800, 1335225600,
1337472000, 1340409600, 1340928000, 1336089600, 1339459200, 1340064000,
1337904000, 1335571200, 1334707200, 1336003200, 1334966400, 1339113600,
1333238400, 1335657600, 1337385600, 1336176000, 1335312000, 1339804800,
1337472000, 1339372800, 1334188800, 1334102400, 1340236800, 1336262400,
1334880000, 1337731200, 1333411200, 1337644800, 1339632000, 1336780800,
1334448000, 1338249600, 1333584000, 1340150400, 1334361600, 1336953600,
1334966400, 1337731200, 1335571200, 1333238400, 1338940800, 1336953600,
1334188800, 1333238400, 1334448000, 1333411200, 1334188800, 1336262400,
1340150400, 1338508800, 1335916800, 1339891200, 1338940800, 1338163200,
1336435200, 1340841600, 1337472000, 1333584000, 1340668800, 1336435200,
1340668800, 1335484800, 1340841600, 1339113600, 1337126400, 1334793600,
1337126400, 1336867200, 1338595200, 1337731200, 1334620800, 1340150400,
1337385600, 1339718400, 1337817600, 1340409600, 1340755200, 1339545600,
1336953600, 1339459200, 1337385600, 1340496000, 1333584000, 1341964800,
1347235200, 1345939200, 1348272000, 1343260800, 1346198400, 1348099200,
1345420800, 1348012800, 1348272000, 1342656000, 1342569600, 1345939200,
1342224000, 1348012800, 1347926400, 1342396800, 1344556800, 1342483200,
1344297600, 1348185600, 1347753600, 1341187200, 1347408000, 1345680000,
1341446400, 1347840000, 1342310400, 1348790400, 1345075200, 1343088000,
1346889600, 1347494400, 1342915200, 1346025600, 1346457600, 1343779200,
1343174400, 1341360000, 1343347200, 1347235200, 1341705600, 1341446400,
1347580800, 1347753600, 1343088000, 1348531200, 1343952000, 1345939200,
1348358400, 1346284800, 1348963200, 1345593600, 1342742400, 1341360000,
1346716800, 1348099200, 1341705600, 1344816000, 1347408000, 1344297600,
1342656000, 1348099200, 1344038400, 1342483200, 1342224000, 1346544000,
1346025600, 1341100800, 1345420800, 1342742400, 1346803200, 1342828800,
1347580800, 1347753600, 1343001600, 1348963200, 1344902400, 1343001600,
1341878400, 1341964800, 1341705600, 1341273600, 1348272000, 1343520000,
1341446400, 1341446400, 1347062400, 1343520000, 1346976000, 1346198400,
1348358400, 1345680000, 1343347200, 1348358400, 1345075200, 1344124800,
1347148800, 1342224000, 1343260800, 1348444800, 1342569600, 1347840000,
1343347200, 1345420800, 1341964800, 1347753600, 1343174400, 1347840000,
1343260800, 1348012800, 1344470400, 1344643200, 1347408000, 1348963200,
1344988800, 1344124800, 1345334400, 1343347200, 1348617600, 1342396800,
1348099200, 1342569600, 1344988800, 1343520000, 1344816000, 1342396800,
1344988800, 1347494400, 1344124800, 1342656000, 1343779200, 1343779200,
1341792000, 1344729600, 1345593600, 1342483200, 1348272000, 1346716800,
1341360000, 1345507200, 1346889600, 1345766400, 1342137600, 1342137600,
1348185600, 1344384000, 1342656000, 1343433600, 1346803200, 1348531200,
1345593600, 1346630400, 1341446400, 1342656000, 1341446400, 1348531200,
1343865600, 1347840000, 1346716800, 1346198400, 1346544000, 1342224000,
1345507200, 1342828800, 1344211200, 1347840000, 1346025600, 1348876800,
1343606400, 1341705600, 1346457600, 1347148800, 1348617600, 1341100800,
1343433600, 1347840000, 1341360000, 1341878400, 1343260800, 1342310400,
1343606400, 1348704000, 1345766400, 1344124800, 1342483200, 1346544000,
1343174400, 1345420800, 1341532800, 1345334400, 1346716800, 1344470400,
1344988800, 1342828800, 1347494400, 1344297600, 1342483200, 1346025600,
1348876800), class = c("POSIXct", "POSIXt", "QDate"), QDate = structure(c(3L,
5L, 4L, 5L, 6L, 1L, 2L, 4L, 2L, 6L, 5L, 2L, 7L, 5L, 6L, 6L, 4L,
7L, 6L, 5L, 6L, 3L, 5L, 4L, 6L, 2L, 4L, 2L, 2L, 7L, 2L, 3L, 2L,
5L, 1L, 7L, 2L, 4L, 6L, 5L, 7L, 2L, 5L, 7L, 6L, 3L, 5L, 5L, 7L,
4L, 2L, 3L, 4L, 7L, 4L, 7L, 7L, 3L, 6L, 4L, 1L, 7L,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
13L, 14L, 10L, 13L, 13L, 11L, 9L, 9L, 10L, 9L, 12L, 7L, 9L, 11L,
10L, 9L, 13L, 11L, 13L, 8L, 8L, 13L, 10L, 9L, 11L, 8L, 11L, 13L,
10L, 8L, 12L, 8L, 13L, 8L, 11L, 9L, 11L, 9L, 7L, 12L, 11L, 8L,
7L, 8L, 8L, 8L, 10L, 13L, 12L, 10L, 13L, 12L, 12L, 10L, 14L,
11L, 8L, 14L, 10L, 14L, 9L, 14L, 12L, 11L, 9L, 11L, 10L, 12L,
11L, 9L, 13L, 11L, 13L, 11L, 13L, 14L, 13L, 11L, 13L, 11L, 13L,
8L, 15L, 19L, 18L, 20L, 16L, 18L, 20L, 18L, 20L, 20L, 15L, 15L,
18L, 15L, 20L, 20L, 15L, 17L, 15L, 17L, 20L, 19L, 14L, 19L, 18L,
14L, 20L, 15L, 20L, 17L, 16L, 19L, 19L, 15L, 18L, 18L, 16L, 16L,
14L, 16L, 19L, 14L, 14L, 19L, 19L, 16L, 20L, 16L, 18L, 20L, 18L,
20L, 18L, 15L, 14L, 19L, 20L, 14L, 17L, 19L, 17L, 15L, 20L, 16L,
15L, 15L, 18L, 18L, 14L, 18L, 15L, 19L, 15L, 19L, 19L, 16L, 20L,
17L, 16L, 15L, 15L, 14L, 14L, 20L, 16L, 14L, 14L, 19L, 16L, 19L,
18L, 20L, 18L, 16L, 20L, 17L, 16L, 19L, 15L, 16L, 20L, 15L, 20L,
16L, 18L, 15L, 19L, 16L, 20L, 16L, 20L, 17L, 17L, 19L, 20L, 17L,
16L, 17L, 16L, 20L, 15L, 20L, 15L, 17L, 16L, 17L, 15L, 17L, 19L,
16L, 15L, 16L, 16L, 15L, 17L, 18L, 15L, 20L, 19L, 14L, 18L, 19L,
18L, 15L, 15L, 20L, 17L, 15L, 16L, 19L, 20L, 18L, 19L, 14L, 15L,
14L, 20L, 16L, 20L, 19L, 18L, 18L, 15L, 18L, 15L, 17L, 20L, 18L,
20L, 16L, 14L, 18L, 19L, 20L, 14L, 16L, 20L, 14L, 15L, 16L, 15L,
16L, 20L, 18L, 16L, 15L, 18L, 16L, 18L, 14L, 17L, 19L, 17L, 17L,
15L, 19L, 17L, 15L, 18L, 20L), class = c("ordered", "factor"), .Label = c("26/12/2011-8/01/2012",
"9/01/2012-22/01/2012", "23/01/2012-5/02/2012", "6/02/2012-19/02/2012",
"20/02/2012-4/03/2012", "5/03/2012-18/03/2012", "19/03/2012-1/04/2012",
"2/04/2012-15/04/2012", "16/04/2012-29/04/2012", "30/04/2012-13/05/2012",
"14/05/2012-27/05/2012", "28/05/2012-10/06/2012", "11/06/2012-24/06/2012",
"25/06/2012-8/07/2012", "9/07/2012-22/07/2012", "23/07/2012-5/08/2012",
"6/08/2012-19/08/2012", "20/08/2012-2/09/2012", "3/09/2012-16/09/2012",
"17/09/2012-30/09/2012")), questiontype = "Date", name = "date", label = "Interview Date", question = "Interview Date")
ww <- rep(c(1,5), each = 5)
test_that("SummarizeVariable",
{
expect_equal(SummarizeVariable(x0, "Sum"), 55)
expect_equal(SummarizeVariable(x0, "Sum", weight = ww), 215)
expect_equal(round(SummarizeVariable(x.logical, "Percentage"),0), structure(67, statistic = "%"))
expect_equal(round(SummarizeVariable(x.logical, "Average"),2), 0.67)
expect_equal(SummarizeVariable(x.logical, "Sum"), 4)
expect_warning(SummarizeVariable(x.logical, "Percentage", category="3"),
"Showing percentage selected")
expect_warning(SummarizeVariable(x.categoric, "Average"),
"Data has been automatically converted to numeric")
expect_equal(round(SummarizeVariable(x.date, "Average"),2), 14705.5)
expect_equal(SummarizeVariable(x.numeric, "Average"), 0.55)
expect_warning(SummarizeVariable(x.numeric, "Percentage", category = 1),
"A numeric variable was supplied")
expect_equal(SummarizeVariable(x.numeric, "Percentage", category = "0.4 - 0.6"), structure(30, statistic = "%"))
expect_error(SummarizeVariable(x.qdate, "Percentage", category = ""),
"Select one or more categories from \"26/12/2011-8/01/2012\", \"9/01/2012-22/01/2012\"")
expect_equal(SummarizeVariable(x.qdate, "Percentage", category = "26/12/2011-8/01/2012"), structure(2, statistic = "%"))
expect_warning(SummarizeVariable(x.categoric.with.comma, "Percentage", category = "$1,000"),
"Categories ") #\"$1\", \"000\" not found. Use double-quotes to surround category names containing commas.")
expect_equal(SummarizeVariable(x.categoric.with.comma, "Percentage", category = "\"$1,000\""),
structure(37.5, statistic = "%"))
})
test_that("missing values",
{
data("phone", package = "flipExampleData")
ff <- phone$choiceid %% 4 == 0
ww <- phone$q1
expect_equal(SummarizeVariable(phone$q8), 1.377279, tol = 1e-5)
expect_equal(SummarizeVariable(phone$q2, "Percentage", category = "Student"),
structure(30.1428571428571, statistic = "%"))
expect_equal(SummarizeVariable(phone$q8, weights = ww, subset = ff),
1.38364779874214, tol = 1e-5)
expect_equal(SummarizeVariable(phone$q2, "Percentage", category = "Retired",
weights = ww, subset = ff), structure(5, statistic = "%"))
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.