pivot_longer.SpatVector {tidyterra} | R Documentation |
Pivot SpatVector
from wide to long
Description
pivot_longer()
"lengthens" data, increasing the number of rows and
decreasing the number of columns. The inverse transformation is
pivot_wider.SpatVector()
Learn more in tidyr::pivot_wider()
.
Usage
## S3 method for class 'SpatVector'
pivot_longer(
data,
cols,
...,
cols_vary = "fastest",
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
)
Arguments
data |
A |
cols |
< |
... |
Additional arguments passed on to methods. |
cols_vary |
When pivoting
|
names_to |
A character vector specifying the new column or columns to
create from the information stored in the column names of
|
names_prefix |
A regular expression used to remove matching text from the start of each variable name. |
names_sep , names_pattern |
If
If these arguments do not give you enough control, use
|
names_ptypes , values_ptypes |
Optionally, a list of column name-prototype
pairs. Alternatively, a single empty prototype can be supplied, which will
be applied to all columns. A prototype (or ptype for short) is a
zero-length vector (like |
names_transform , values_transform |
Optionally, a list of column
name-function pairs. Alternatively, a single function can be supplied,
which will be applied to all columns. Use these arguments if you need to
change the types of specific columns. For example, If not specified, the type of the columns generated from |
names_repair |
What happens if the output has invalid column names?
The default, |
values_to |
A string specifying the name of the column to create
from the data stored in cell values. If |
values_drop_na |
If |
Value
A SpatVector
object.
Methods
Implementation of the generic tidyr::pivot_longer()
function.
SpatVector
The geometry column has a sticky behavior. This means that the result would
have always the geometry of data
.
See Also
Other tidyr verbs for pivoting:
pivot_wider.SpatVector()
Other tidyr methods:
drop_na.Spat
,
fill.SpatVector()
,
pivot_wider.SpatVector()
,
replace_na.Spat
Examples
library(dplyr)
library(tidyr)
library(ggplot2)
library(terra)
temp <- rast((system.file("extdata/cyl_temp.tif", package = "tidyterra")))
cyl <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) %>%
project(temp)
# Add average temp
temps <- terra::extract(temp, cyl, fun = "mean", na.rm = TRUE, xy = TRUE)
cyl_temp <- cbind(cyl, temps) %>%
glimpse()
# And pivot long for plot
cyl_temp %>%
pivot_longer(
cols = tavg_04:tavg_06,
names_to = "label",
values_to = "temp"
) %>%
ggplot() +
geom_spatvector(aes(fill = temp)) +
facet_wrap(~label, ncol = 1) +
scale_fill_whitebox_c(palette = "muted")