From 7321b317a091b066b98d25e183d2071b3baa33d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Lu=C5=A1trik?= Date: Wed, 15 Jul 2020 17:04:21 +0200 Subject: [PATCH] Fix coercion of factors to integers Fixes and closes #6 --- R/peep.R | 10 +++---- tests/testthat/valid_3rows | 18 ++++++----- tests/testthat/valid_extran | 60 ++++++++++++++++++++----------------- tests/testthat/valid_full | 47 ++++++++++++++++------------- tests/testthat/valid_r2c | 46 +++++++++++++++------------- 5 files changed, 98 insertions(+), 83 deletions(-) diff --git a/R/peep.R b/R/peep.R index 10ff0a1..093448f 100644 --- a/R/peep.R +++ b/R/peep.R @@ -32,7 +32,7 @@ peep <- function(x, n = 6, digits = 4, r2c = FALSE) { } if (!is.data.frame(x)) { - x <- data.frame(x, check.names = FALSE) + x <- data.frame(x, check.names = FALSE, stringsAsFactors = FALSE) } # Subset to only the first and last 50 columns. There's a chance @@ -123,7 +123,6 @@ peep <- function(x, n = 6, digits = 4, r2c = FALSE) { if (length(columns) >= ncol(x)) { out <- clipAndAddHorizontalDivider(x = x, dot = dot, n = n) - print(out) return(out) } @@ -138,7 +137,8 @@ peep <- function(x, n = 6, digits = 4, r2c = FALSE) { for (i in columns[-1]) { const.out <- cbind(const.out, x[, i, drop = FALSE]) } - x <- do.call(cbind, const.out) + + x <- const.out x <- as.data.frame(x, stringsAsFactors = FALSE) rownames(x) <- save.rownames @@ -150,7 +150,5 @@ peep <- function(x, n = 6, digits = 4, r2c = FALSE) { # Add horizontal divider. out <- clipAndAddHorizontalDivider(x = x, dot = dot, n = n) - print(out) - - return(invisible(out)) + return(out) } diff --git a/tests/testthat/valid_3rows b/tests/testthat/valid_3rows index e79d8df..cee9bff 100644 --- a/tests/testthat/valid_3rows +++ b/tests/testthat/valid_3rows @@ -1,7 +1,11 @@ -valid.3rows <- structure(list(mpg = c(16, 16, 19), cyl = c(6, 6, 4), disp = c(160, -160, 108), hp = c(110, 110, 93), drat = c(3.9, 3.9, 3.85), wt = c(2.62, -2.875, 2.32), qsec = c(16.46, 17.02, 18.61), ` ` = c("·", "·", -"·"), cyl.1 = c(6, 6, 4), disp.1 = c(160, 160, 108), hp.1 = c(110, -110, 93), drat.1 = c(3.9, 3.9, 3.85), drat.2 = c(3.9, 3.9, 3.85 -), drat.3 = c(3.9, 3.9, 3.85)), row.names = c("1:", "2:", "3:" -), class = "data.frame") +valid.3rows <- structure(list(mpg = structure(c(16L, 16L, 19L), .Label = c("10.4", +"13.3", "14.3", "14.7", "15", "15.2", "15.5", "15.8", "16.4", +"17.3", "17.8", "18.1", "18.7", "19.2", "19.7", "21", "21.4", +"21.5", "22.8", "24.4", "26", "27.3", "30.4", "32.4", "33.9"), class = "factor"), + cyl = c(6, 6, 4), disp = c(160, 160, 108), hp = c(110, 110, + 93), drat = c(3.9, 3.9, 3.85), wt = c(2.62, 2.875, 2.32), + qsec = c(16.46, 17.02, 18.61), ` ` = c("·", "·", "·"), + cyl.1 = c(6, 6, 4), disp.1 = c(160, 160, 108), hp.1 = c(110, + 110, 93), drat.1 = c(3.9, 3.9, 3.85), drat.2 = c(3.9, 3.9, + 3.85), drat.3 = c(3.9, 3.9, 3.85)), row.names = c("1:", "2:", +"3:"), class = "data.frame") diff --git a/tests/testthat/valid_extran b/tests/testthat/valid_extran index 34f71ab..eea3976 100644 --- a/tests/testthat/valid_extran +++ b/tests/testthat/valid_extran @@ -1,28 +1,32 @@ -valid.extra.n <- structure(list(mpg = c("16", "16", "19", "17", "13", "12", "3", -"20", "·", "14", "22", "21", "23", "8", "15", "5", "17"), cyl = c("6", -"6", "4", "6", "8", "6", "8", "4", "·", "8", "4", "4", "4", -"8", "6", "8", "4"), disp = c("160", "160", "108", "258", "360", -"225", "360", "146.7", "·", "400", "79", "120.3", "95.1", "351", -"145", "301", "121"), hp = c("110", "110", "93", "110", "175", -"105", "245", "62", "·", "175", "66", "91", "113", "264", "175", -"335", "109"), drat = c("3.9", "3.9", "3.85", "3.08", "3.15", -"2.76", "3.21", "3.69", "·", "3.08", "4.08", "4.43", "3.77", -"4.22", "3.62", "3.54", "4.11"), wt = c("2.62", "2.875", "2.32", -"3.215", "3.44", "3.46", "3.57", "3.19", "·", "3.845", "1.935", -"2.14", "1.513", "3.17", "2.77", "3.57", "2.78"), ` ` = c("·", -"·", "·", "·", "·", "·", "·", "·", "·", "·", "·", "·", -"·", "·", "·", "·", "·"), cyl.1 = c("6", "6", "4", "6", -"8", "6", "8", "4", "·", "8", "4", "4", "4", "8", "6", "8", -"4"), disp.1 = c("160", "160", "108", "258", "360", "225", "360", -"146.7", "·", "400", "79", "120.3", "95.1", "351", "145", "301", -"121"), hp.1 = c("110", "110", "93", "110", "175", "105", "245", -"62", "·", "175", "66", "91", "113", "264", "175", "335", "109" -), drat.1 = c("3.9", "3.9", "3.85", "3.08", "3.15", "2.76", "3.21", -"3.69", "·", "3.08", "4.08", "4.43", "3.77", "4.22", "3.62", -"3.54", "4.11"), drat.2 = c("3.9", "3.9", "3.85", "3.08", "3.15", -"2.76", "3.21", "3.69", "·", "3.08", "4.08", "4.43", "3.77", -"4.22", "3.62", "3.54", "4.11"), drat.3 = c("3.9", "3.9", "3.85", -"3.08", "3.15", "2.76", "3.21", "3.69", "·", "3.08", "4.08", -"4.43", "3.77", "4.22", "3.62", "3.54", "4.11")), row.names = c("01:", -"02:", "03:", "04:", "05:", "06:", "07:", "08:", "", "25:", "26:", -"27:", "28:", "29:", "30:", "31:", "32:"), class = "data.frame") +valid.extra.n <- structure(list(mpg = structure(c(16L, 16L, 19L, 17L, 13L, 12L, +3L, 20L, 26L, 14L, 22L, 21L, 23L, 8L, 15L, 5L, 17L), .Label = c("10.4", +"13.3", "14.3", "14.7", "15", "15.2", "15.5", "15.8", "16.4", +"17.3", "17.8", "18.1", "18.7", "19.2", "19.7", "21", "21.4", +"21.5", "22.8", "24.4", "26", "27.3", "30.4", "32.4", "33.9", +"·"), class = "factor"), cyl = c("6", "6", "4", "6", "8", "6", +"8", "4", "·", "8", "4", "4", "4", "8", "6", "8", "4"), disp = c("160", +"160", "108", "258", "360", "225", "360", "146.7", "·", "400", +"79", "120.3", "95.1", "351", "145", "301", "121"), hp = c("110", +"110", "93", "110", "175", "105", "245", "62", "·", "175", "66", +"91", "113", "264", "175", "335", "109"), drat = c("3.9", "3.9", +"3.85", "3.08", "3.15", "2.76", "3.21", "3.69", "·", "3.08", +"4.08", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11"), wt = c("2.62", +"2.875", "2.32", "3.215", "3.44", "3.46", "3.57", "3.19", "·", +"3.845", "1.935", "2.14", "1.513", "3.17", "2.77", "3.57", "2.78" +), ` ` = c("·", "·", "·", "·", "·", "·", "·", "·", "·", +"·", "·", "·", "·", "·", "·", "·", "·"), cyl.1 = c("6", +"6", "4", "6", "8", "6", "8", "4", "·", "8", "4", "4", "4", +"8", "6", "8", "4"), disp.1 = c("160", "160", "108", "258", "360", +"225", "360", "146.7", "·", "400", "79", "120.3", "95.1", "351", +"145", "301", "121"), hp.1 = c("110", "110", "93", "110", "175", +"105", "245", "62", "·", "175", "66", "91", "113", "264", "175", +"335", "109"), drat.1 = c("3.9", "3.9", "3.85", "3.08", "3.15", +"2.76", "3.21", "3.69", "·", "3.08", "4.08", "4.43", "3.77", +"4.22", "3.62", "3.54", "4.11"), drat.2 = c("3.9", "3.9", "3.85", +"3.08", "3.15", "2.76", "3.21", "3.69", "·", "3.08", "4.08", +"4.43", "3.77", "4.22", "3.62", "3.54", "4.11"), drat.3 = c("3.9", +"3.9", "3.85", "3.08", "3.15", "2.76", "3.21", "3.69", "·", +"3.08", "4.08", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11" +)), row.names = c("01:", "02:", "03:", "04:", "05:", "06:", "07:", +"08:", "", "25:", "26:", "27:", "28:", "29:", "30:", "31:", "32:" +), class = "data.frame") diff --git a/tests/testthat/valid_full b/tests/testthat/valid_full index 2df6a24..57f6054 100644 --- a/tests/testthat/valid_full +++ b/tests/testthat/valid_full @@ -1,22 +1,27 @@ -valid.full <- structure(list(mpg = c("16", "16", "19", "17", "13", "12", "·", -"21", "23", "8", "15", "5", "17"), cyl = c("6", "6", "4", "6", -"8", "6", "·", "4", "4", "8", "6", "8", "4"), disp = c("160", -"160", "108", "258", "360", "225", "·", "120.3", "95.1", "351", -"145", "301", "121"), hp = c("110", "110", "93", "110", "175", -"105", "·", "91", "113", "264", "175", "335", "109"), drat = c("3.9", -"3.9", "3.85", "3.08", "3.15", "2.76", "·", "4.43", "3.77", -"4.22", "3.62", "3.54", "4.11"), wt = c("2.62", "2.875", "2.32", -"3.215", "3.44", "3.46", "·", "2.14", "1.513", "3.17", "2.77", -"3.57", "2.78"), ` ` = c("·", "·", "·", "·", "·", "·", -"·", "·", "·", "·", "·", "·", "·"), cyl.1 = c("6", "6", -"4", "6", "8", "6", "·", "4", "4", "8", "6", "8", "4"), disp.1 = c("160", -"160", "108", "258", "360", "225", "·", "120.3", "95.1", "351", -"145", "301", "121"), hp.1 = c("110", "110", "93", "110", "175", -"105", "·", "91", "113", "264", "175", "335", "109"), drat.1 = c("3.9", -"3.9", "3.85", "3.08", "3.15", "2.76", "·", "4.43", "3.77", -"4.22", "3.62", "3.54", "4.11"), drat.2 = c("3.9", "3.9", "3.85", -"3.08", "3.15", "2.76", "·", "4.43", "3.77", "4.22", "3.62", -"3.54", "4.11"), drat.3 = c("3.9", "3.9", "3.85", "3.08", "3.15", -"2.76", "·", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11")), row.names = c("01:", -"02:", "03:", "04:", "05:", "06:", "", "27:", "28:", "29:", "30:", +valid.full <- structure(list(mpg = structure(c(16L, 16L, 19L, 17L, 13L, 12L, +26L, 21L, 23L, 8L, 15L, 5L, 17L), .Label = c("10.4", "13.3", +"14.3", "14.7", "15", "15.2", "15.5", "15.8", "16.4", "17.3", +"17.8", "18.1", "18.7", "19.2", "19.7", "21", "21.4", "21.5", +"22.8", "24.4", "26", "27.3", "30.4", "32.4", "33.9", "·"), class = "factor"), + cyl = c("6", "6", "4", "6", "8", "6", "·", "4", "4", "8", + "6", "8", "4"), disp = c("160", "160", "108", "258", "360", + "225", "·", "120.3", "95.1", "351", "145", "301", "121"), + hp = c("110", "110", "93", "110", "175", "105", "·", "91", + "113", "264", "175", "335", "109"), drat = c("3.9", "3.9", + "3.85", "3.08", "3.15", "2.76", "·", "4.43", "3.77", "4.22", + "3.62", "3.54", "4.11"), wt = c("2.62", "2.875", "2.32", + "3.215", "3.44", "3.46", "·", "2.14", "1.513", "3.17", "2.77", + "3.57", "2.78"), ` ` = c("·", "·", "·", "·", "·", "·", + "·", "·", "·", "·", "·", "·", "·"), cyl.1 = c("6", + "6", "4", "6", "8", "6", "·", "4", "4", "8", "6", "8", "4" + ), disp.1 = c("160", "160", "108", "258", "360", "225", "·", + "120.3", "95.1", "351", "145", "301", "121"), hp.1 = c("110", + "110", "93", "110", "175", "105", "·", "91", "113", "264", + "175", "335", "109"), drat.1 = c("3.9", "3.9", "3.85", "3.08", + "3.15", "2.76", "·", "4.43", "3.77", "4.22", "3.62", "3.54", + "4.11"), drat.2 = c("3.9", "3.9", "3.85", "3.08", "3.15", + "2.76", "·", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11" + ), drat.3 = c("3.9", "3.9", "3.85", "3.08", "3.15", "2.76", + "·", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11")), row.names = c("01:", +"02:", "03:", "04:", "05:", "06:", "", "27:", "28:", "29:", "30:", "31:", "32:"), class = "data.frame") diff --git a/tests/testthat/valid_r2c b/tests/testthat/valid_r2c index 6d011cd..e980c9c 100644 --- a/tests/testthat/valid_r2c +++ b/tests/testthat/valid_r2c @@ -1,21 +1,25 @@ -valid.r2c <- structure(list(rn = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", -"Hornet 4 Drive", "Hornet Sportabout", "Valiant", "·", "Porsche 914-2", -"Lotus Europa", "Ford Pantera L", "Ferrari Dino", "Maserati Bora", -"Volvo 142E"), mpg = c("16", "16", "19", "17", "13", "12", "·", -"21", "23", "8", "15", "5", "17"), cyl = c("6", "6", "4", "6", -"8", "6", "·", "4", "4", "8", "6", "8", "4"), disp = c("160", -"160", "108", "258", "360", "225", "·", "120.3", "95.1", "351", -"145", "301", "121"), hp = c("110", "110", "93", "110", "175", -"105", "·", "91", "113", "264", "175", "335", "109"), ` ` = c("·", -"·", "·", "·", "·", "·", "·", "·", "·", "·", "·", "·", -"·"), disp.1 = c("160", "160", "108", "258", "360", "225", "·", -"120.3", "95.1", "351", "145", "301", "121"), hp.1 = c("110", -"110", "93", "110", "175", "105", "·", "91", "113", "264", "175", -"335", "109"), drat.1 = c("3.9", "3.9", "3.85", "3.08", "3.15", -"2.76", "·", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11"), - drat.2 = c("3.9", "3.9", "3.85", "3.08", "3.15", "2.76", - "·", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11"), drat.3 = c("3.9", - "3.9", "3.85", "3.08", "3.15", "2.76", "·", "4.43", "3.77", - "4.22", "3.62", "3.54", "4.11")), row.names = c("01:", "02:", -"03:", "04:", "05:", "06:", "", "27:", "28:", "29:", "30:", "31:", -"32:"), class = "data.frame") +valid.r2c <- structure(list(rn = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", +"Hornet 4 Drive", "Hornet Sportabout", "Valiant", "·", "Porsche 914-2", +"Lotus Europa", "Ford Pantera L", "Ferrari Dino", "Maserati Bora", +"Volvo 142E"), mpg = structure(c(16L, 16L, 19L, 17L, 13L, 12L, +26L, 21L, 23L, 8L, 15L, 5L, 17L), .Label = c("10.4", "13.3", +"14.3", "14.7", "15", "15.2", "15.5", "15.8", "16.4", "17.3", +"17.8", "18.1", "18.7", "19.2", "19.7", "21", "21.4", "21.5", +"22.8", "24.4", "26", "27.3", "30.4", "32.4", "33.9", "·"), class = "factor"), + cyl = c("6", "6", "4", "6", "8", "6", "·", "4", "4", "8", + "6", "8", "4"), disp = c("160", "160", "108", "258", "360", + "225", "·", "120.3", "95.1", "351", "145", "301", "121"), + hp = c("110", "110", "93", "110", "175", "105", "·", "91", + "113", "264", "175", "335", "109"), ` ` = c("·", "·", + "·", "·", "·", "·", "·", "·", "·", "·", "·", "·", + "·"), disp.1 = c("160", "160", "108", "258", "360", "225", + "·", "120.3", "95.1", "351", "145", "301", "121"), hp.1 = c("110", + "110", "93", "110", "175", "105", "·", "91", "113", "264", + "175", "335", "109"), drat.1 = c("3.9", "3.9", "3.85", "3.08", + "3.15", "2.76", "·", "4.43", "3.77", "4.22", "3.62", "3.54", + "4.11"), drat.2 = c("3.9", "3.9", "3.85", "3.08", "3.15", + "2.76", "·", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11" + ), drat.3 = c("3.9", "3.9", "3.85", "3.08", "3.15", "2.76", + "·", "4.43", "3.77", "4.22", "3.62", "3.54", "4.11")), row.names = c("01:", +"02:", "03:", "04:", "05:", "06:", "", "27:", "28:", "29:", "30:", +"31:", "32:"), class = "data.frame")