File: test_convertListOfRowsToDataFrame.R

package info (click to toggle)
r-cran-bbmisc 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,256 kB
  • sloc: ansic: 176; sh: 9; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 2,813 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
context("convertListOfRowstoDataFrame")

test_that("convertListOfRowstoDataFrame", {
  df1 = convertListOfRowsToDataFrame(list(list(x = 1, y = "a"), list(x = 2, y = "b")),
    strings.as.factors = FALSE)
  df2 = data.frame(x = 1:2, y = c("a", "b"), stringsAsFactors = FALSE)
  expect_equal(df1, df2)

  df1 = convertListOfRowsToDataFrame(list(c(x = "1", y = "a"), list(x = "2", y = "b")),
    strings.as.factors = FALSE)
  df2 = data.frame(x=c("1", "2"), y=c("a", "b"), stringsAsFactors = FALSE)
  expect_equal(df1, df2)

  df1 = convertListOfRowsToDataFrame(list(c("1", "a"), c("2", "b")), strings.as.factors = FALSE,
    col.names=c("x", "y"))
  df2 = data.frame(x=c("1", "2"), y=c("a", "b"), stringsAsFactors = FALSE)
  expect_equal(df1, df2)

  df1 = convertListOfRowsToDataFrame(list(list(a = 1, b = 1), list(b = 12)))
  df2 = convertListOfRowsToDataFrame(list(c(a = 1, b = 1), c(b = 12)))
  expect_equal(df1, df2)

  # names
  df1 = convertListOfRowsToDataFrame(list(list(x = 1, y = "a"), list(x = 2, y = "b")),
    strings.as.factors = FALSE, row.names = c("r1", "r2"))
  df2 = setRowNames(data.frame(x = 1:2, y = c("a", "b"), stringsAsFactors = FALSE), c("r1", "r2"))
  expect_equal(df1, df2)

  df1 = convertListOfRowsToDataFrame(list(list(x = 1, y = "a"), list(x = 2, y = "b")),
    strings.as.factors = FALSE, row.names = 1:2)
  df2 = setRowNames(data.frame(x = 1:2, y = c("a", "b"), stringsAsFactors = FALSE), 1:2)
  expect_equal(df1, df2)

  df1 = convertListOfRowsToDataFrame(list(list(x = 1, y = "a"), list(x = 2, y = "b")),
    strings.as.factors = FALSE, col.names = c("c1", "c2"))
  df2 = data.frame(c1 = 1:2, c2 = c("a", "b"), stringsAsFactors = FALSE)
  expect_equal(df1, df2)

  df1 = convertListOfRowsToDataFrame(list(list(x = 1, y = "a"), list(x = 2, y = "b")),
    strings.as.factors = FALSE, col.names = 1:2)
  df2 = setColNames(data.frame(1:2, c("a", "b"), stringsAsFactors = FALSE), 1:2)
  expect_equal(df1, df2)
})


test_that("convertListOfRowsToDataFrame works with missing stuff in the rows", {

  df1 = setColNames(data.frame(1:2, c("a", NA), stringsAsFactors = FALSE), 1:2)
  df2 = convertListOfRowsToDataFrame(list(list(x = 1, y = "a"), list(x = 2)),
    strings.as.factors = FALSE, col.names = 1:2)
  df3 = convertListOfRowsToDataFrame(list(list(x = 1, y = "a"), list(x = 2, y = NULL)),
    strings.as.factors = FALSE, col.names = 1:2)
  expect_equal(df1, df2)
  expect_equal(df1, df3)

  df1 = setColNames(data.frame(c(1, NA), c(NA, NA), stringsAsFactors = FALSE), 1:2)
  df2 = convertListOfRowsToDataFrame(list(list(x = 1), list(y = NULL)),
    strings.as.factors = FALSE, col.names = 1:2)
  df3 = convertListOfRowsToDataFrame(list(list(x = 1, y = NULL), list(y = NULL)),
    strings.as.factors = FALSE, col.names = 1:2)
  expect_equal(df1, df2)
  expect_equal(df1, df3)
})