Once data is in nabular
form, where the shadow is bound to the data, it
can be useful to reshape it into a long format with the shadow columns
in a separate grouping - so you have variable
, value
, and
variable_NA
and value_NA
.
Arguments
- shadow_data
a data.frame
- ...
bare name of variables that you want to focus on
- fn_value_transform
function to transform the "value" column. Default is NULL, which defaults to
as.character
. Be aware thatas.numeric
may fail for some instances if it cannot coerce the value into numeric. See the examples.- only_main_vars
logical - do you want to filter down to main variables?
Examples
aq_shadow <- nabular(airquality)
shadow_long(aq_shadow)
#> # A tibble: 918 × 4
#> variable value variable_NA value_NA
#> <chr> <chr> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Wind 7.4 Wind_NA !NA
#> 4 Temp 67 Temp_NA !NA
#> 5 Month 5 Month_NA !NA
#> 6 Day 1 Day_NA !NA
#> 7 Ozone 36 Ozone_NA !NA
#> 8 Solar.R 118 Solar.R_NA !NA
#> 9 Wind 8 Wind_NA !NA
#> 10 Temp 72 Temp_NA !NA
#> # ℹ 908 more rows
# then filter only on Ozone
shadow_long(aq_shadow, Ozone)
#> # A tibble: 153 × 4
#> variable value variable_NA value_NA
#> <chr> <chr> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Ozone 36 Ozone_NA !NA
#> 3 Ozone 12 Ozone_NA !NA
#> 4 Ozone 18 Ozone_NA !NA
#> 5 Ozone NA Ozone_NA NA
#> 6 Ozone 28 Ozone_NA !NA
#> 7 Ozone 23 Ozone_NA !NA
#> 8 Ozone 19 Ozone_NA !NA
#> 9 Ozone 8 Ozone_NA !NA
#> 10 Ozone NA Ozone_NA NA
#> # ℹ 143 more rows
shadow_long(aq_shadow, Ozone, Solar.R)
#> # A tibble: 306 × 4
#> variable value variable_NA value_NA
#> <chr> <chr> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Ozone 36 Ozone_NA !NA
#> 4 Solar.R 118 Solar.R_NA !NA
#> 5 Ozone 12 Ozone_NA !NA
#> 6 Solar.R 149 Solar.R_NA !NA
#> 7 Ozone 18 Ozone_NA !NA
#> 8 Solar.R 313 Solar.R_NA !NA
#> 9 Ozone NA Ozone_NA NA
#> 10 Solar.R NA Solar.R_NA NA
#> # ℹ 296 more rows
# ensure `value` is numeric
shadow_long(aq_shadow, fn_value_transform = as.numeric)
#> # A tibble: 918 × 4
#> variable value variable_NA value_NA
#> <chr> <dbl> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Wind 7.4 Wind_NA !NA
#> 4 Temp 67 Temp_NA !NA
#> 5 Month 5 Month_NA !NA
#> 6 Day 1 Day_NA !NA
#> 7 Ozone 36 Ozone_NA !NA
#> 8 Solar.R 118 Solar.R_NA !NA
#> 9 Wind 8 Wind_NA !NA
#> 10 Temp 72 Temp_NA !NA
#> # ℹ 908 more rows
shadow_long(aq_shadow, Ozone, Solar.R, fn_value_transform = as.numeric)
#> # A tibble: 306 × 4
#> variable value variable_NA value_NA
#> <chr> <dbl> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Ozone 36 Ozone_NA !NA
#> 4 Solar.R 118 Solar.R_NA !NA
#> 5 Ozone 12 Ozone_NA !NA
#> 6 Solar.R 149 Solar.R_NA !NA
#> 7 Ozone 18 Ozone_NA !NA
#> 8 Solar.R 313 Solar.R_NA !NA
#> 9 Ozone NA Ozone_NA NA
#> 10 Solar.R NA Solar.R_NA NA
#> # ℹ 296 more rows