4
4
# ' and metadata. It can be seen as a snapshot of a data set that contains the
5
5
# ' most up-to-date values of some signal variables of interest, as of a given
6
6
# ' time.
7
- # '
8
- # ' @details An `epi_df` is a tibble with (at least) the following columns:
9
- # '
7
+ # '
8
+ # ' @details An `epi_df` is a tibble with (at least) the following columns:
9
+ # '
10
10
# ' * `geo_value`: the geographic value associated with each row of measurements.
11
11
# ' * `time_value`: the time value associated with each row of measurements.
12
12
# '
13
13
# ' Other columns can be considered as measured variables, which we also refer to
14
14
# ' as signal variables. An `epi_df` object also has metadata with (at least)
15
- # ' the following fields:
16
- # '
15
+ # ' the following fields:
16
+ # '
17
17
# ' * `geo_type`: the type for the geo values.
18
18
# ' * `time_type`: the type for the time values.
19
19
# ' * `as_of`: the time value at which the given data were available.
42
42
# '
43
43
# ' @section Geo Types:
44
44
# ' The following geo types are recognized in an `epi_df`.
45
- # '
45
+ # '
46
46
# ' * `"county"`: each observation corresponds to a U.S. county; coded by 5-digit
47
- # ' FIPS code.
47
+ # ' FIPS code.
48
48
# ' * `"hrr"`: each observation corresponds to a U.S. hospital referral region
49
49
# ' (designed to represent regional healthcare markets); there are 306 HRRs in
50
50
# ' the U.S; coded by number (nonconsecutive, between 1 and 457).
51
51
# ' * `"state"`: each observation corresponds to a U.S. state; coded by 2-digit
52
- # ' postal abbreviation (lowercase);
53
- # ' note that Puerto Rico is "pr" and Washington D.C. is "dc".
52
+ # ' postal abbreviation (lowercase);
53
+ # ' note that Puerto Rico is "pr" and Washington D.C. is "dc".
54
54
# ' * `"hhs"`: each observation corresponds to a U.S. HHS region; coded by number
55
55
# ' (consecutive, between 1 and 10).
56
56
# ' * `"nation"`: each observation corresponds to a country; coded by ISO 31661-
57
57
# ' alpha-2 country codes (lowercase).
58
58
# '
59
59
# ' An unrecognizable geo type is labeled "custom".
60
- # '
60
+ # '
61
61
# ' @section Time Types:
62
62
# ' The following time types are recognized in an `epi_df`.
63
- # '
63
+ # '
64
64
# ' * `"day-time"`: each observation corresponds to a time on a given day
65
65
# ' (measured to the second); coded as a `POSIXct` object, as in
66
66
# ' `as.POSIXct("2022-01-31 18:45:40")`.
71
71
# ' `Date` object, representing the start date of week.
72
72
# ' * `"yearweek"`: each observation corresponds to a week; the alignment can be
73
73
# ' arbitrary; coded as a `tsibble::yearweek` object, where the alignment is
74
- # ' stored in the `week_start` field of its attributes.
74
+ # ' stored in the `week_start` field of its attributes.
75
75
# ' * `"yearmonth"`: each observation corresponds to a month; coded as a
76
76
# ' `tsibble::yearmonth` object.
77
77
# ' * `"yearquarter"`: each observation corresponds to a quarter; coded as a
108
108
# ' well.
109
109
# ' @param ... Additional arguments passed to methods.
110
110
# ' @return An `epi_df` object.
111
- # '
111
+ # '
112
112
# ' @export
113
113
as_epi_df = function (x , ... ) {
114
114
UseMethod(" as_epi_df" )
@@ -158,13 +158,13 @@ as_epi_df.tbl_df = function(x, geo_type, time_type, as_of,
158
158
" as_of" %in% names(attributes(x )$ metadata )) {
159
159
as_of = attributes(x )$ metadata $ as_of
160
160
}
161
-
161
+
162
162
# Next check for as_of, issue, or version columns
163
163
else if (" as_of" %in% names(x )) as_of = max(x $ as_of )
164
164
else if (" issue" %in% names(x )) as_of = max(x $ issue )
165
165
else if (" version" %in% names(x )) as_of = max(x $ version )
166
166
167
- # If we got here then we failed
167
+ # If we got here then we failed
168
168
else as_of = Sys.time() # Use the current day-time
169
169
}
170
170
@@ -174,10 +174,10 @@ as_epi_df.tbl_df = function(x, geo_type, time_type, as_of,
174
174
metadata $ time_type = time_type
175
175
metadata $ as_of = as_of
176
176
metadata = c(metadata , additional_metadata )
177
-
178
- # Reorder columns (geo_value, time_value, ...)
177
+
178
+ # Reorder columns (geo_value, time_value, ...)
179
179
x = dplyr :: relocate(x , .data $ geo_value , .data $ time_value )
180
-
180
+
181
181
# Apply epi_df class, attach metadata, and return
182
182
class(x ) = c(" epi_df" , class(x ))
183
183
attributes(x )$ metadata = metadata
@@ -209,3 +209,13 @@ as_epi_df.tbl_ts = function(x, geo_type, time_type, as_of,
209
209
as_epi_df.tbl_df(tibble :: as_tibble(x ), geo_type , time_type , as_of ,
210
210
additional_metadata , ... )
211
211
}
212
+
213
+ # ' Test for `epi_df` format
214
+ # '
215
+ # ' @param x An object.
216
+ # ' @return `TRUE` if the object inherits from `epi_df`.
217
+ # '
218
+ # ' @export
219
+ is_epi_df = function (x ) {
220
+ inherits(x , " epi_df" )
221
+ }
0 commit comments