forked from tidyverse/tidyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-gather.R
205 lines (167 loc) · 5.33 KB
/
test-gather.R
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
test_that("gather all columns when ... is empty", {
df <- data.frame(
x = 1:5,
y = 6:10
)
out <- gather(df, key, val)
expect_equal(nrow(out), 10)
expect_equal(names(out), c("key", "val"))
})
test_that("gather returns input if no columns gathered", {
df <- data.frame(x = 1:2, y = 1:2)
out <- gather(df, a, b, -x, -y)
expect_equal(df, out)
})
test_that("if not supply, key and value default to key and value", {
df <- data.frame(x = 1:2)
out <- gather(df)
expect_equal(nrow(out), 2)
expect_equal(names(out), c("key", "value"))
})
test_that("Missing values removed when na.rm = TRUE", {
df <- data.frame(x = c(1, NA))
out <- gather(df, k, v)
expect_equal(out$v, df$x)
out <- gather(df, k, v, na.rm = TRUE)
expect_equal(out$v, 1)
})
test_that("key converted to character by default", {
df <- data.frame(y = 1, x = 2)
out <- gather(df, k, v)
expect_equal(out$k, c("y", "x"))
})
test_that("covert will generate integers if needed", {
df <- tibble(`1` = 1, `2` = 2)
out <- gather(df, convert = TRUE)
expect_identical(out$key, c(1L, 2L))
})
test_that("key preserves column ordering when factor_key = TRUE", {
df <- data.frame(y = 1, x = 2)
out <- gather(df, k, v, factor_key = TRUE)
expect_equal(out$k, factor(c("y", "x"), levels = c("y", "x")))
})
test_that("preserve class of input", {
dat <- data.frame(x = 1:2)
dat %>%
as_tibble() %>%
gather() %>%
expect_s3_class("tbl_df")
})
test_that("additional inputs control which columns to gather", {
data <- tibble(a = 1, b1 = 1, b2 = 2, b3 = 3)
out <- gather(data, key, val, b1:b3)
expect_equal(names(out), c("a", "key", "val"))
expect_equal(out$val, 1:3)
})
test_that("group_vars are kept where possible", {
df <- tibble(x = 1, y = 1, z = 1)
# Can't keep
out <- df %>%
dplyr::group_by(x) %>%
gather(key, val, x:z)
expect_equal(out, tibble(key = c("x", "y", "z"), val = 1))
# Can keep
out <- df %>%
dplyr::group_by(x) %>%
gather(key, val, y:z)
expect_equal(dplyr::group_vars(out), "x")
})
test_that("overwrites existing vars", {
df <- data.frame(
X = 1,
Y = 1,
Z = 2
)
rs <- gather(df, key = "name", value = "Y")
expect_named(rs, c("name", "Y"))
expect_equal(rs$Y, c(1, 2))
})
# Column types ------------------------------------------------------------
test_that("can gather all atomic vectors", {
df1 <- data.frame(x = 1, y = FALSE)
df2 <- data.frame(x = 1, y = 1L)
df3 <- data.frame(x = 1, y = 1)
df4 <- data.frame(x = 1, y = "a", stringsAsFactors = FALSE)
df5 <- data.frame(x = 1, y = 1 + 1i, stringsAsFactors = FALSE)
gathered_val <- function(val) {
data.frame(x = 1, key = "y", val = val, stringsAsFactors = FALSE)
}
gathered_key <- function(key) {
data.frame(y = key, key = "x", val = 1, stringsAsFactors = FALSE)
}
expect_equal(gather(df1, key, val, -x), gathered_val(FALSE))
expect_equal(gather(df2, key, val, -x), gathered_val(1L))
expect_equal(gather(df3, key, val, -x), gathered_val(1))
expect_equal(gather(df4, key, val, -x), gathered_val("a"))
expect_equal(gather(df5, key, val, -x), gathered_val(1 + 1i))
expect_equal(gather(df1, key, val, -y), gathered_key(FALSE))
expect_equal(gather(df2, key, val, -y), gathered_key(1L))
expect_equal(gather(df3, key, val, -y), gathered_key(1))
expect_equal(gather(df4, key, val, -y), gathered_key("a"))
expect_equal(gather(df5, key, val, -y), gathered_key(1 + 1i))
})
test_that("gather throws error for POSIXlt", {
df <- data.frame(y = 1)
df$x <- as.POSIXlt(Sys.time())
expect_snapshot(error = TRUE, cnd_class = TRUE, {
gather(df, key, val, -x)
gather(df, key, val, -y)
})
})
test_that("gather throws error for weird objects", {
df <- data.frame(y = 1)
df$x <- expression(x)
# Can't use snapshot, it changes between versions of R
expect_error(gather(d, key, val, -x), "not found")
expect_snapshot(error = TRUE, {
gather(df, key, val, -y)
})
e <- new.env(parent = emptyenv())
e$x <- 1
df <- data.frame(y = 1)
df$x <- e
expect_snapshot(error = TRUE, cnd_class = TRUE, {
gather(df, key, val, -x)
gather(df, key, val, -y)
})
})
test_that("factors coerced to characters, not integers", {
df <- data.frame(
v1 = 1:3,
v2 = factor(letters[1:3])
)
expect_snapshot(out <- gather(df, k, v))
expect_equal(out$v, c(1:3, letters[1:3]))
})
test_that("attributes of id variables are preserved", {
df <- data.frame(x = factor(1:3), y = 1:3, z = 3:1)
out <- gather(df, key, val, -x)
expect_equal(attributes(df$x), attributes(out$x))
})
test_that("common attributes are preserved", {
df <- data.frame(date1 = Sys.Date(), date2 = Sys.Date() + 10)
out <- gather(df, k, v)
expect_s3_class(out$v, "Date")
})
test_that("varying attributes are dropped with a warning", {
df <- data.frame(
date1 = as.POSIXct("2019-01-01", tz = "UTC"),
date2 = as.Date("2019-01-01")
)
expect_snapshot(gather(df, k, v))
})
test_that("gather preserves OBJECT bit on e.g. POSIXct", {
df <- data.frame(now = Sys.time())
out <- gather(df, k, v)
expect_true(is.object(out$v))
})
test_that("can handle list-columns", {
df <- tibble(x = 1:2, y = list("a", TRUE))
out <- gather(df, k, v, -y)
expect_identical(out$y, df$y)
})
test_that("can gather list-columns", {
df <- tibble(x = 1:2, y = list(1, 2), z = list(3, 4))
out <- gather(df, k, v, y:z)
expect_equal(out$v, list(1, 2, 3, 4))
})