/
group-data-SpatVector.R
211 lines (190 loc) · 4.1 KB
/
group-data-SpatVector.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
206
207
208
209
210
211
#' Grouping metadata for `SpatVector` objects
#'
#' @description
#' This collection of functions accesses data about grouped `SpatVector` objects
#' in various ways:
#'
#' * [group_data()] returns a tibble that defines the grouping structure.
#' The columns give the values of the grouping variables. The last column,
#' always called `.rows`, is a list of integer vectors that gives the
#' location of the rows in each group.
#'
#' * [group_keys()] returns a tibble describing the groups.
#'
#' * [group_rows()] returns a list of integer vectors giving the rows that
#' each group contains.
#'
#' * [group_indices()] returns an integer vector the same length as `.data`
#' that gives the group that each row belongs to.
#'
#' * [group_vars()] gives names of grouping variables as character vector.
#'
#' * [groups()] gives the names of the grouping variables as a list of symbols.
#'
#' * [group_size()] gives the size of each group.
#'
#' * [n_groups()] gives the total number of groups.
#'
#' See [dplyr::group_data()].
#'
#' @param .data,.tbl,x A `SpatVector`.
#' @inheritParams dplyr::group_data
#'
#' @keywords internal
#'
#' @return
#'
#' See the description of the function. The results are usually tibbles,
#' lists or vectors. These functions does not return `SpatVector` objects.
#'
#' @rdname group_data.SpatVector
#' @name group_data.SpatVector
#'
#' @export
#' @importFrom dplyr group_data
#'
#' @examples
#' library(terra)
#'
#' v <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
#' v$gr_1 <- rep_len(c("A", "A", "B"), length.out = nrow(v))
#' v$gr_2 <- rep_len(c("C", "D"), length.out = nrow(v))
#'
#' # Ungrouped
#'
#' n_groups(v)
#'
#' group_vars(v)
#'
#' group_keys(v)
#'
#' group_size(v)
#'
#' groups(v)
#'
#' group_rows(v)
#'
#' group_data(v)
#'
#' group_indices(v)
#'
#' # Grouped by one var
#' gv <- group_by(v, gr_1)
#'
#' n_groups(gv)
#'
#' group_vars(gv)
#'
#' group_keys(gv)
#'
#' group_size(gv)
#'
#' groups(gv)
#'
#' group_rows(gv)
#'
#' group_data(gv)
#'
#' group_indices(gv)
#'
#' # Grouped by several vars
#'
#' gv2 <- group_by(v, gr_1, gr_2)
#'
#' n_groups(gv2)
#'
#' group_vars(gv2)
#'
#' group_keys(gv2)
#'
#' group_size(gv2)
#'
#' groups(gv2)
#'
#' group_rows(gv2)
#'
#' group_data(gv2)
#'
#' group_indices(gv2)
group_data.SpatVector <- function(.data) {
# Dispatch to dplyr
dplyr::group_data(tbl_for_groups(.data))
}
#' @export
dplyr::group_data
#' @export
#' @rdname group_data.SpatVector
#' @importFrom dplyr group_keys
group_keys.SpatVector <- function(.tbl, ...) {
# Dispatch to dplyr
dplyr::group_keys(tbl_for_groups(.tbl, ...))
}
#' @export
dplyr::group_keys
#' @importFrom dplyr group_rows
#' @export
dplyr::group_rows
#' @export
#' @rdname group_data.SpatVector
#' @importFrom dplyr group_indices
group_indices.SpatVector <- function(.data, ...) {
# Dispatch to dplyr
dplyr::group_indices(tbl_for_groups(.data), ...)
}
#' @export
dplyr::group_indices
#' @export
#' @rdname group_data.SpatVector
#' @importFrom dplyr group_vars
group_vars.SpatVector <- function(x) {
# Dispatch to dplyr
dplyr::group_vars(tbl_for_groups(x))
}
#' @export
dplyr::group_vars
#' @export
#' @rdname group_data.SpatVector
#' @importFrom dplyr groups
groups.SpatVector <- function(x) {
# Dispatch to dplyr
dplyr::groups(tbl_for_groups(x))
}
#' @export
dplyr::groups
#' @export
#' @rdname group_data.SpatVector
#' @importFrom dplyr group_size
group_size.SpatVector <- function(x) {
# Dispatch to dplyr
dplyr::group_size(tbl_for_groups(x))
}
#' @export
dplyr::group_size
#' @export
dplyr::groups
#' @export
#' @rdname group_data.SpatVector
#' @importFrom dplyr n_groups
n_groups.SpatVector <- function(x) {
# Dispatch to dplyr
dplyr::n_groups(tbl_for_groups(x))
}
#' @export
dplyr::n_groups
# Helper
tbl_for_groups <- function(x) {
df <- tibble::as_tibble(terra::as.data.frame(x))
# Grouped
if (is_grouped_spatvector(x)) {
# Add class
class(df) <- c("grouped_df", class(df))
attr(df, "groups") <- attr(x, "groups")
}
# Grouped
if (is_rowwise_spatvector(x)) {
# Add class
class(df) <- c("rowwise_df", class(df))
attr(df, "groups") <- attr(x, "groups")
}
return(df)
}