df1 <- data.frame(
name = c("Amy","Tony","Jessica"),
age = c(18,21,30),
hobby = c("lab","quiz","swim")
)
df2 <- data.frame(
name = c(NA,NA,NA),
age = c(NA,NA,NA),
hobby = c(NA,NA,NA)
)
df3 <- data.frame()
df4 <- data.frame(
name = c(NA,NA,"Jessica"),
age = c(NA,21,30),
hobby = c("lab","quiz","swim")
)
# test for the input type
test_that("The input should be a dataframe", {
expect_error(miss_count(list(1, "str", c(1, 2, 3))))
expect_error(miss_count("a"))
})
# test for empty dataframe
test_that("The input dataframe is empty", {
expect_error(miss_count(df3))
})
# test for counting and percentage results with 2 other edge cases
test_that("The output dataframe is not correct", {
expect_true(all.equal(miss_count(df1)$counts,c(0,0,0)))
expect_true(all.equal(miss_count(df1)$percentage,c(0,0,0)))
expect_true(all.equal(miss_count(df2)$count,c(3,3,3)))
expect_true(all.equal(miss_count(df2)$percentage,c(1,1,1)))
})
# test for descending
test_that("The order of output dataframe is not correct", {
expect_true(all.equal(miss_count(df4)$counts,c(2,1,0)))
expect_true(all.equal(miss_count(df4)$percentage,c(0.67,0.33,0.00)))
expect_true(all.equal(rownames(miss_count(df4)),c("name","age","hobby")))
})
# test for asscending
test_that("The order of output dataframe is not correct", {
expect_true(all.equal(miss_count(df4,ascending=TRUE)$counts,c(0,1,2)))
expect_true(all.equal(miss_count(df4,ascending=TRUE)$percentage,c(0.00,0.33,0.67)))
expect_true(all.equal(rownames(miss_count(df4,ascending=TRUE)),c("hobby","age","name")))
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.