Skip to contents

Similar in spirit to tidyr::gather(), but does NOT put the values in the gathered columns into one very long "value"-column while labeling the different original columns in a very long "key"-column -- instead it creates a tfd-column containing the functional measurements of the columns given in ....

Usage

tf_gather(
  data,
  ...,
  key = ".tfd",
  arg = NULL,
  domain = NULL,
  evaluator = tf_approx_linear,
  resolution = NULL
)

Arguments

data

a data frame -- note that dplyr does not handle matrix columns well, if data contains more than one of those, tf_gather will fail...

...

A selection of columns to collect as a tfd object. Each column represents measurements of a functional variable at a specific arg-val. Can also be the name of a matrix-valued column, but see above. If empty, all variables are selected. You can supply bare variable names, select all variables between x and z with x:z, exclude y with -y. For more options, see the dplyr::select() documentation.

key

the name of the created tfd-column. Defaults to ".tfd", but the function will try to guess the name based on the column names of the gathered columns in this case. If a common prefix of all column names is found, this is used instead. You also get a message about this.

arg

optional. Argument values for the functions. If not provided, will be guessed from the column names as well. See also tf::tfd().

domain

optional. Range of possible arg-values. See tf::tfd() for details.

evaluator

optional. A function accepting arguments x, arg, evaluations. See tf::tfd() for details.

resolution

optional. Resolution of the evaluation grid in arg. See tf::tfd() for details.

Value

a modified data.frame with a tfd column replacing the ....

See also

dplyr::select()

Other tidyfun data wrangling functions: tf_evaluate.data.frame(), tf_nest(), tf_spread(), tf_unnest()

Examples

(d <- dplyr::as.tbl(data.frame(refund::DTI[1:5, ]$cca[, 1:10])))
#> Warning: `as.tbl()` was deprecated in dplyr 1.0.0.
#>  Please use `tibble::as_tibble()` instead.
#> # A tibble: 5 × 10
#>   cca_1 cca_2 cca_3 cca_4 cca_5 cca_6 cca_7 cca_8 cca_9 cca_10
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
#> 1 0.491 0.517 0.536 0.555 0.593 0.633 0.650 0.656 0.649  0.638
#> 2 0.472 0.487 0.502 0.523 0.552 0.587 0.597 0.603 0.592  0.579
#> 3 0.502 0.514 0.539 0.574 0.603 0.634 0.636 0.635 0.620  0.609
#> 4 0.402 0.423 0.440 0.460 0.475 0.499 0.517 0.555 0.583  0.601
#> 5 0.402 0.406 0.399 0.386 0.409 0.425 0.430 0.445 0.487  0.510
tf_gather(d)
#> creating new tfd-column <cca>
#> # A tibble: 5 × 1
#>                                 cca
#>                           <tfd_reg>
#> 1 [1]: (1,0.5);(2,0.5);(3,0.5); ...
#> 2 [2]: (1,0.5);(2,0.5);(3,0.5); ...
#> 3 [3]: (1,0.5);(2,0.5);(3,0.5); ...
#> 4 [4]: (1,0.4);(2,0.4);(3,0.4); ...
#> 5 [5]: (1,0.4);(2,0.4);(3,0.4); ...
tf_gather(d, key = "cca_tf")
#> # A tibble: 5 × 1
#>                              cca_tf
#>                           <tfd_reg>
#> 1 [1]: (1,0.5);(2,0.5);(3,0.5); ...
#> 2 [2]: (1,0.5);(2,0.5);(3,0.5); ...
#> 3 [3]: (1,0.5);(2,0.5);(3,0.5); ...
#> 4 [4]: (1,0.4);(2,0.4);(3,0.4); ...
#> 5 [5]: (1,0.4);(2,0.4);(3,0.4); ...
tf_gather(d, arg = seq(0, 1, length.out = 10))$cca
#> creating new tfd-column <cca>
#> tfd[5] on (0,1) based on 10 evaluations each
#> interpolation by tf_approx_linear 
#> [1]: (0.00,0.49);(0.11,0.52);(0.22,0.54); ...
#> [2]: (0.00,0.47);(0.11,0.49);(0.22,0.50); ...
#> [3]: (0.00,0.50);(0.11,0.51);(0.22,0.54); ...
#> [4]: (0.00,0.40);(0.11,0.42);(0.22,0.44); ...
#> [5]: (0.00,0.40);(0.11,0.41);(0.22,0.40); ...
(d2 <- dplyr::bind_cols(id = rownames(d), d))
#> # A tibble: 5 × 11
#>   id    cca_1 cca_2 cca_3 cca_4 cca_5 cca_6 cca_7 cca_8 cca_9 cca_10
#>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
#> 1 1     0.491 0.517 0.536 0.555 0.593 0.633 0.650 0.656 0.649  0.638
#> 2 2     0.472 0.487 0.502 0.523 0.552 0.587 0.597 0.603 0.592  0.579
#> 3 3     0.502 0.514 0.539 0.574 0.603 0.634 0.636 0.635 0.620  0.609
#> 4 4     0.402 0.423 0.440 0.460 0.475 0.499 0.517 0.555 0.583  0.601
#> 5 5     0.402 0.406 0.399 0.386 0.409 0.425 0.430 0.445 0.487  0.510
tf_gather(d2, -id) # tf_gather(d2, matches("cca")); tf_gather(d2, -1); etc
#> creating new tfd-column <cca>
#> # A tibble: 5 × 2
#>   id                                  cca
#>   <chr>                         <tfd_reg>
#> 1 1     [1]: (1,0.5);(2,0.5);(3,0.5); ...
#> 2 2     [2]: (1,0.5);(2,0.5);(3,0.5); ...
#> 3 3     [3]: (1,0.5);(2,0.5);(3,0.5); ...
#> 4 4     [4]: (1,0.4);(2,0.4);(3,0.4); ...
#> 5 5     [5]: (1,0.4);(2,0.4);(3,0.4); ...