to_long: Convert wide format weather data to long format

Description Usage Arguments Details Value Examples

View source: R/to_long.R

Description

to_long transforms wide-formatted data, where each column respresents an observation for a specific day for a specific site, to wide-formatted data, where each row corresponds to an observation for a single day for a specific site.

Usage

1
to_long(data, keep_cols = 1, date_sep = "_")

Arguments

data

wide format data

keep_cols

columns in data to retain in long format; all other columns will be converted to key/value pairs in long format; defaults to 1. This should most likely correspond to the index of the column containing the site unique identifier

date_sep

character used to delimit variable prefix from date in column names; defaults to underscore ("_")

Details

to_long assumes data has a column indicating site unique identifier (i.e. a site ID column) and remaining columns are daily measurements of a variable in question (i.e rainfall, temperature). Remaining column names are assumed to have a prefix, a separator character, and date in YYYYMMDD format. For example, rainfall data column names would be something like "rf_19830325". Character strings occuring before delimiter are not used. The character string in column names corresponding to date are used to extract the date (YYYYMMDD).

If to_long encounters invalid dates (e.g. 30 February 1984), such dates will be assigned a value of NA in resultant data frame and a warning will be thrown.

Value

data frame where each row corresponds to an observation for a single day. Measurements are in the column value and date is in the column date. Measurements retain the class of original data, and date is of class Date.

Examples

1
2
3
4
5
## Not run: 
# to_long is not exported, so we use triple-colon (:::)
rain_long <- wxsumR:::to_long(data = rain_2yr)

## End(Not run)

jcoliver/weathercommand documentation built on Sept. 12, 2021, 3:28 a.m.