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 ....

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", and 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

If not provided, will be guessed from the column names as well. See tfd().

domain

range of the arg.

evaluator

a function accepting arguments x, arg, evaluations. See details for tfd().

resolution

resolution of the evaluation grid. See details for tfd().

Value

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

See also

dplyr::select() tfd() tf_nest() tf_unnest()

Examples

(d <- dplyr::as.tbl(data.frame(refund::DTI[1:5,]$cca[, 1:10])))
#> # A tibble: 5 x 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 x 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 x 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, l = 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 x 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 x 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); ...