File: tidyverse.R

package info (click to toggle)
r-cran-sftime 0.2-0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 452 kB
  • sloc: sh: 13; makefile: 2
file content (292 lines) | stat: -rw-r--r-- 8,728 bytes parent folder | download | duplicates (2)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Tidyverse methods (See also join.R)

#' 'tidyverse' methods for \code{sftime} objects
#'
#' 'tidyverse' methods for \code{sftime} objects. Geometries are sticky, use 
#' \code{\link{as.data.frame}} to let \code{dplyr}'s own methods drop them. Use 
#' these methods without the \code{.sftime} suffix and after loading the 
#' 'tidyverse' package with the generic (or after loading package 'tidyverse').
#' @name tidyverse
#' @inheritParams sf::tidyverse
#' @inheritParams tidyr::pivot_longer
#' @param x An object of class \code{sftime}.
#' @param .data An object of class \code{stime}.
#' @return 
#' \itemize{
#'   \item For \code{_join} methods: An object of class \code{sftime} 
#'   representing the joining result of \code{x} and \code{y}. See 
#'   \code{\link[dplyr]{mutate-joins}}.
#'   \item For \code{filter}: See \code{\link[dplyr]{filter}}.
#'   \item For \code{arrange}: See \code{\link[dplyr]{arrange}}. 
#'   \item For \code{group_by} and \code{ungroup}: A grouped \code{sftime} 
#'   object. See \code{\link[dplyr]{arrange}}. 
#'   \item For \code{rowwise}: An \code{sftime} object. See 
#'   \code{\link[dplyr]{rowwise}}. 
#'   \item For \code{mutate} and \code{transmute}: See 
#'   \code{\link[dplyr]{mutate}}.
#'   \item For \code{select}: See \code{\link[dplyr]{select}}. If the active 
#'   time column is not explicitly selected, a \code{sf} object is returned. 
#'   \item For \code{rename}: See \code{\link[dplyr]{rename}}.    
#'   \item For \code{slice}: See \code{\link[dplyr]{slice}}.
#'   \item For \code{summarize} and \code{summarise}: See 
#'   \code{\link[dplyr]{summarise}}.  
#'   \item For \code{distinct}: See \code{\link[dplyr]{distinct}}.
#'   \item For \code{gather}: See \code{\link[tidyr]{gather}}. 
#' }
#' 
NULL

#' @rdname tidyverse
#' @examples
#' ## filter
#' filter(x1, a <= 2)
#' 
 filter.sftime <- function(.data, ..., .dots) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## arrange
#' arrange(x1, dplyr::desc(a))
#' 
 arrange.sftime <- function(.data, ..., .dots) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## group_by
#' group_by(x1, time)
#' 
 group_by.sftime <- function(.data, ..., add = FALSE) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## ungroup
#' ungroup(group_by(x1, time))
#' 
 ungroup.sftime <- function(.data, ...) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## rowwise
#' x1 %>%
#'   mutate(a1 = 5:7) %>%
#'   rowwise() %>%
#'   mutate(a2 = mean(a, a1))
#' 
 rowwise.sftime <- function(.data, ...) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## mutate
#' x1 %>%
#'   mutate(a1 = 5:7)
#' 
 mutate.sftime <- function(.data, ..., .dots) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## transmute
#' x1 %>%
#'   transmute(a1 = 5:7)
#' 
 transmute.sftime <- function(.data, ..., .dots) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## select
#' x1 %>%
#'   select(-time) %>%
#'   select(geometry)
#' 
 select.sftime <- function(.data, ...) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## rename
#' x1 %>%
#'   rename(a1 = a)
#' 
 rename.sftime <- function(.data, ...) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## slice
#' x1 %>%
#'   slice(1:2)
#' 
 slice.sftime <- function(.data, ..., .dots) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## summarise
#' x1 %>%
#'   summarise(time = mean(time))
#'   
#' x1 %>%
#'   summarize(time = mean(time))
#' 
 summarise.sftime <- function(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
 summarize.sftime <- summarise.sftime

#' @rdname tidyverse
#' @examples
#' ## distinct
#' x1 %>%
#'   distinct(geometry)
#' 
 distinct.sftime <- function(.data, ..., .keep_all = FALSE) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## gather
#' library(tidyr)
#' x1 %>%
#'   mutate(a1 = 5:7) %>%
#'   gather(key = "variable", value = "value", a, a1)
#' 
 gather.sftime <- function(data, key, value, ..., na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
  reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## pivot_longer
#' x1 %>%
#'   mutate(a1 = 5:7) %>%
#'   pivot_longer(cols = c("a", "a1"), names_to = "variable", values_to = "value")
#' 
 pivot_longer.sftime <- function (data, cols, names_to = "name", names_prefix = NULL,
                             names_sep = NULL, names_pattern = NULL, names_ptypes = NULL,
                             names_transform = NULL, names_repair = "check_unique",
                             values_to = "value", values_drop_na = FALSE, values_ptypes = NULL,
                             values_transform = NULL, ...) {
  reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## spread
#' x1 %>%
#'   mutate(a1 = 5:7) %>%
#'   gather(key = "variable", value = "value", a, a1) %>%
#'   spread(key = "variable", value = "value")
#' 
 spread.sftime <- function(data, key, value, fill = NA, convert = FALSE, drop = TRUE,
                      sep = NULL) {
  reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## sample_n
#' set.seed(234)
#' x1 %>%
#'   sample_n(size = 10, replace = TRUE)
#' 
 sample_n.sftime <- function(tbl, size, replace = FALSE, weight = NULL, .env = parent.frame()) {
  reclass_sftime(NextMethod(), time_column_name = attr(tbl, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## sample_frac
#' x1 %>%
#'   sample_frac(size = 10, replace = TRUE) %>%
#'   sample_frac(size = 0.1, replace = FALSE)
#' 
 sample_frac.sftime <- function(tbl, size = 1, replace = FALSE, weight = NULL, .env = parent.frame()) {
  reclass_sftime(NextMethod(), time_column_name = attr(tbl, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## nest
#' x1 %>%
#'   nest(a1 = -time)
#' 
 nest.sftime <- function (.data, ...) {
  reclass_sftime(NextMethod(), time_column_name = attr(.data, "time_column"))
}

#' @name tidyverse
#' @examples
#' ## unnest
#' x1 %>%
#'   mutate(a1 = list(1, c(1, 2), 5)) %>%
#'   unnest(a1)
#' 
 unnest.sftime = function(data, ..., .preserve = NULL) {
  reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## separate
#' x1 %>%
#'   mutate(x = c(NA, "a.b", "a.d")) %>%
#'   separate(x, c("A", "B"))
#' 
 separate.sftime <- function(data, col, into, sep = "[^[:alnum:]]+", remove = TRUE,
                       convert = FALSE, extra = "warn", fill = "warn", ...) {
  
  time_column_name <- attr(data, "time_column")
  class(data) <- setdiff(class(data), "sftime")
  
  # modified from sftime (tidyverse.R)
  if (!requireNamespace("rlang", quietly = TRUE))
    stop("rlang required: install first?")
  col <- rlang::enquo(col)
  
  res <- tidyr::separate(data, !!col, into = into,
                  sep = sep, remove = remove, convert = convert, extra = extra, fill = fill, ...)
  reclass_sftime(res, time_column_name = time_column_name)
  
}

#' @name tidyverse
#' @examples
#' ## unite
#' x1 %>%
#'   mutate(x = c(NA, "a.b", "a.d")) %>%
#'   separate(x, c("A", "B")) %>%
#'   unite(x, c("A", "B"))
#'   
unite.sftime <- function(data, col, ..., sep = "_", remove = TRUE) {
  reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column"))
}

#' @rdname tidyverse
#' @examples
#' ## separate_rows
#' x1 %>%
#'   mutate(z = c("1", "2,3,4", "5,6")) %>%
#'   separate_rows(z, convert = TRUE)
#' 
 separate_rows.sftime <- function(data, ..., sep = "[^[:alnum:]]+", convert = FALSE) {
  reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column"))
}