Skip to content

Commit 1418e51

Browse files
authored
Merge pull request #381 from adokter/as.data.frame
Fix #364
2 parents e717b19 + be71284 commit 1418e51

File tree

5 files changed

+112
-238
lines changed

5 files changed

+112
-238
lines changed

R/as.data.frame.R

+57-111
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
#' Convert a vertical profile (\code{vp}) to a Data Frame
1+
#' Convert a vertical profile (`vp`) or time series of vertical profiles
2+
#' (`vpts`) to a data frame
23
#'
3-
#' Converts a vertical profile to a Data Frame, and optionally adds information
4-
#' on sunrise/sunset, day/night and derived quantities like migration
5-
#' traffic rates.
4+
#' Converts a vertical profile (`vp`) or a time series of vertical profiles
5+
#' (`vpts`) to a data frame containing all quantities per datetime and height.
6+
#' Has options to include latitude/longitude/antenna height (parameter `geo`)
7+
#' and day/sunrise/sunset (parameter `suntime`).
68
#'
7-
#' @param x An object of class \code{vp}.
8-
#' @param row.names \code{NULL} or a character vector giving the row names for
9+
#' @param x A `vp` or `vpts` object.
10+
#' @param row.names `NULL` or a character vector giving the row names for
911
#' the data frame. Missing values are not allowed. See [base::as.data.frame()].
10-
#' @param optional If \code{FALSE} then the names of the variables in the data
12+
#' @param optional Logical. If `FALSE` then the names of the variables in the data
1113
#' frame are checked to ensure that they are syntactically valid variable names
12-
#' and are not duplicated.
13-
#' @param quantities An optional character vector with the names of the
14-
#' quantities to include as columns in the data frame.
15-
#' @param elev Sun elevation in degrees, see \link{sunrise}/\link{sunset}.
16-
#' @param lat Radar latitude in decimal degrees. When set, overrides the
17-
#' latitude stored in \code{x} in \link{sunrise}/\link{sunset} calculations
18-
#' @param lon Radar longitude in decimal degrees. When set, overrides the
19-
#' longitude stored in \code{x} in \link{sunrise}/\link{sunset} calculations.
20-
#' @param suntime Logical, when \code{TRUE}, adds sunrise/sunset and day/night
21-
#' information to each row.
22-
#' @param geo Logical, when \code{TRUE}, adds latitude, longitude and antenna
23-
#' height of the radar to each row.
14+
#' and are not duplicated. See [base::as.data.frame()].
15+
#' @param geo Logical. When `TRUE`, adds latitude (`lat`), longitude (`lon`) and
16+
#' antenna height of the radar (`height_antenna`) to each row.
17+
#' @param suntime Logical. When `TRUE`, adds whether it is daytime (`day`) and
18+
#' the datetime of `sunrise` and `sunset` to each row.
19+
#' @param lat Numeric. Radar latitude in decimal degrees. When set, overrides
20+
#' the latitude stored in `x` for [sunrise()]/[sunset()] calculations.
21+
#' @param lon Numeric. Radar longitude in decimal degrees. When set, overrides
22+
#' the longitude stored in `x` for [sunrise()]/[sunset()] calculations.
23+
#' @param elev Numeric. Sun elevation in degrees, used for
24+
#' [sunrise()]/[sunset()] calculations.
2425
#' @param ... Additional arguments to be passed to or from methods.
2526
#'
26-
#' @return An object of class \code{data.frame}.
27+
#' @return A `data.frame` object.
2728
#'
2829
#' @export
2930
#'
3031
#' @details
31-
#' Note that only the "dens" quantity is thresholded for radial velocity
32-
#' standard deviation by \link{sd_vvp_threshold}. Note that this is different from the
33-
#' default \link{plot.vp}, \link{plot.vpts} and \link{get_quantity.vp}
34-
#' functions, where quantities "eta", "dbz", "ff", "u", "v", "w", "dd" are
35-
#' all thresholded by \link{sd_vvp_threshold}
32+
#' Note that only the `dens` quantity is thresholded for radial velocity
33+
#' standard deviation by [sd_vvp_threshold()]. This is different from the
34+
#' default [plot.vp()], [plot.vpts()] and [get_quantity()] functions, where
35+
#' quantities `eta`, `dbz`, `ff`, `u`, `v`, `w`, `dd` are all thresholded by
36+
#' [sd_vvp_threshold()].
3637
#'
3738
#' @examples
3839
#' # Load the example vertical profile
@@ -44,25 +45,36 @@
4445
#' # Print data.frame
4546
#' vp_df
4647
#'
47-
#' # Do not compute sunrise/sunset information
48-
#' vp_df <- as.data.frame(vp, suntime = FALSE)
48+
#' # Load the example time series of vertical profiles
49+
#' vpts <- example_vpts
50+
#'
51+
#' # Convert to a data.frame
52+
#' vpts_df <- as.data.frame(vpts)
53+
#'
54+
#' # Print the first 5 rows of the data.frame
55+
#' vpts_df[1:5, ]
56+
#'
57+
#' # Do not add lat/lon/height_antenna information
58+
#' vpts_df <- as.data.frame(vpts, geo = FALSE)
59+
#'
60+
#' # Do not add day/sunrise/sunset information
61+
#' vpts_df <- as.data.frame(vpts, suntime = FALSE)
4962
#'
5063
#' # Override the latitude/longitude information stored in the object when
5164
#' # calculating sunrise/sunset information
52-
#' vp_df <- as.data.frame(vp, suntime = TRUE, lat = 50, lon = 4)
53-
as.data.frame.vp <- function(x, row.names = NULL, optional = FALSE,
54-
quantities = names(x$data), suntime = TRUE,
55-
geo = TRUE, elev = -0.268, lat = NULL,
56-
lon = NULL, ...) {
65+
#' vpts_df <- as.data.frame(vpts, lat = 50, lon = 4)
66+
as.data.frame.vp <- function(x, row.names = NULL, optional = FALSE, geo = TRUE,
67+
suntime = TRUE, lat = NULL, lon = NULL,
68+
elev = -0.268, ...) {
5769
stopifnot(inherits(x, "vp"))
5870
if (!is.null(row.names)) {
5971
if (is.character(row.names) & length(row.names) ==
6072
length(x$datetime) * length(x$height)) {
6173
rownames(output) <- row.names
6274
} else {
63-
stop(paste(
64-
"`row.names` is not a character vector of length",
65-
length(x$datetime) * length(x$height)
75+
stop(paste0(
76+
"`row.names` is not a character vector of length ",
77+
length(x$datetime) * length(x$data$height), "."
6678
))
6779
}
6880
}
@@ -72,14 +84,6 @@ as.data.frame.vp <- function(x, row.names = NULL, optional = FALSE,
7284
if (is.null(lon)) {
7385
lon <- x$attributes$where$lon
7486
}
75-
missing <- which(!(quantities %in% names(x$data)))
76-
if (length(missing) > 0) {
77-
stop(paste(
78-
paste(quantities[missing], collapse = " "),
79-
"not an available quantity, select one or more of",
80-
paste(names(x$data), collapse = ",")
81-
))
82-
}
8387
# coerce data to a data frame
8488
output <- as.data.frame(x$data, optional = optional, ...)
8589
# add height and datetime as a column
@@ -93,7 +97,7 @@ as.data.frame.vp <- function(x, row.names = NULL, optional = FALSE,
9397
output$lon <- lon
9498
output$height_antenna <- x$attributes$where$height
9599
}
96-
# override the lat,lon attributes in case of user-provided values
100+
# override the lat, lon attributes in case of user-provided values
97101
x$attributes$where$lat <- lat
98102
x$attributes$where$lon <- lon
99103
# add day
@@ -115,71 +119,21 @@ as.data.frame.vp <- function(x, row.names = NULL, optional = FALSE,
115119
output
116120
}
117121

118-
#' Convert a time series of vertical profiles (\code{vpts}) to a data frame
119-
#'
120-
#' Converts vertical profile time series (objects of class \code{vpts}) to a
121-
#' data Frame, and optionally adds information on sunrise/sunset, day/night
122-
#' and derived quantities like migration traffic rates.
123-
#'
124-
#' @param x An object of class \code{vpts}.
125-
#' @param row.names \code{NULL} or a character vector giving the row names for
126-
#' the data frame. Missing values are not allowed.
127-
#' @param optional If \code{FALSE} then the names of the variables in the data
128-
#' frame are checked to ensure that they are syntactically valid variable names
129-
#' and are not duplicated.
130-
#' @param quantities An optional character vector with the names of the
131-
#' quantities to include as columns in the data frame.
132-
#' @param elev Sun elevation in degrees, see \link{sunrise}/\link{sunset}.
133-
#' @param lat Radar latitude in decimal degrees. When set, overrides the
134-
#' latitude stored in \code{x} in \link{sunrise}/\link{sunset} calculations.
135-
#' @param lon Radar longitude in decimal degrees. When set, overrides the
136-
#' longitude stored in \code{x} in \link{sunrise}/\link{sunset} calculations.
137-
#' @param suntime Logical, when TRUE, adds sunrise/sunset and day/night
138-
#' information to each row.
139-
#' @param geo Logical, when TRUE, adds latitude, longitude and antenna height
140-
#' of the radar to each row.
141-
#' @param ... Additional arguments to be passed to or from methods.
142-
#'
143-
#' @return An object of class data.frame.
122+
#' @rdname as.data.frame.vp
144123
#'
145124
#' @export
146-
#'
147-
#' @details
148-
#' Note that only the 'dens' quantity is thresholded for radial velocity
149-
#' standard deviation by \link{sd_vvp_threshold}. Note that this is different from the
150-
#' default \link{plot.vp}, \link{plot.vpts} and \link{get_quantity.vp}
151-
#' functions, where quantities "eta", "dbz", "ff", "u", "v", "w", "dd" are all
152-
#' thresholded by \link{sd_vvp_threshold}.
153-
#'
154-
#' @examples
155-
#' # Load the example time series of vertical profiles
156-
#' vpts <- example_vpts
157-
#'
158-
#' # Convert to a data.frame
159-
#' vpts_df <- as.data.frame(vpts)
160-
#'
161-
#' # Print the first 10 rows of the data.frame
162-
#' vpts_df[1:10, ]
163-
#'
164-
#' # Do not compute sunrise/sunset information
165-
#' vpts_df <- as.data.frame(vpts, suntime = FALSE)
166-
#'
167-
#' # Override the latitude/longitude information stored in the object when
168-
#' # calculating sunrise/sunset information
169-
#' vpts_df <- as.data.frame(vpts, suntime = TRUE, lat = 50, lon = 4)
170-
as.data.frame.vpts <- function(x, row.names = NULL, optional = FALSE,
171-
quantities = names(x$data), suntime = TRUE,
172-
geo = TRUE, elev = -0.268, lat = NULL,
173-
lon = NULL, ...) {
125+
as.data.frame.vpts <- function(x, row.names = NULL, optional = FALSE, geo = TRUE,
126+
suntime = TRUE, lat = NULL, lon = NULL,
127+
elev = -0.268, ...) {
174128
stopifnot(inherits(x, "vpts"))
175129
if (!is.null(row.names)) {
176130
if (is.character(row.names) & length(row.names) ==
177131
length(x$datetime) * length(x$height)) {
178132
rownames(output) <- row.names
179133
} else {
180-
stop(paste(
181-
"'row.names' is not a character vector of length",
182-
length(x$datetime) * length(x$height)
134+
stop(paste0(
135+
"`row.names` is not a character vector of length ",
136+
length(x$datetime) * length(x$height), "."
183137
))
184138
}
185139
}
@@ -189,16 +143,8 @@ as.data.frame.vpts <- function(x, row.names = NULL, optional = FALSE,
189143
if (is.null(lon)) {
190144
lon <- x$attributes$where$lon
191145
}
192-
missing <- which(!(quantities %in% names(x$data)))
193-
if (length(missing) > 0) {
194-
stop(paste(
195-
paste(quantities[missing], collapse = " "),
196-
"not an available quantity, select one or more of",
197-
paste(names(x$data), collapse = ",")
198-
))
199-
}
200146
# coerce data to a data frame
201-
output <- as.data.frame(lapply(x$data[quantities], c),
147+
output <- as.data.frame(lapply(x$data, c),
202148
optional = optional, ...
203149
)
204150
# add height and datetime as a column
@@ -217,7 +163,7 @@ as.data.frame.vpts <- function(x, row.names = NULL, optional = FALSE,
217163
output$lon <- lon
218164
output$height_antenna <- x$attributes$where$height
219165
}
220-
# override the lat,lon attributes in case of user-provided values
166+
# override the lat, lon attributes in case of user-provided values
221167
x$attributes$where$lat <- lat
222168
x$attributes$where$lon <- lon
223169
# add day

_pkgdown.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ reference:
9292
- example_vpts
9393
- plot.vpts
9494
- regularize_vpts
95-
- as.data.frame.vpts
95+
- as.data.frame.vp
9696
- "`[.vpts`"
9797
- get_quantity
9898
- title: "Integrating vertical profiles"

man/as.data.frame.vp.Rd

+54-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)