base gather mimics basic functionality of tidyr::gather

b_gather(
  data,
  key = "key",
  value = "value",
  columns = NULL,
  regex = FALSE,
  ...,
  na.rm = FALSE,
  convert = FALSE
)

Arguments

data

data.frame

key

character, name of new key column, Default: 'key'

value

character, name of new value column, Default: 'value'

columns

column names or indicies or regex of them to gather, Default: NULL

regex

boolean, indicates of columns is to be treated as a regular expression, Default: FALSE

...

parameters to pass to grep

na.rm

boolean, apply na.omit to value column, Default: FALSE

convert

boolean, apply type.convert to key column, Default: FALSE

Value

data.frame

See also

Examples

mini_iris <- iris[c(1, 51, 101), ] # gather Sepal.Length, Sepal.Width, Petal.Length, Petal.Width b_gather(mini_iris, key = 'flower_att', value = 'measurement', columns = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width'))
#> Species flower_att measurement #> 1 setosa Sepal.Length 5.1 #> 51 versicolor Sepal.Length 7.0 #> 101 virginica Sepal.Length 6.3 #> 11 setosa Sepal.Width 3.5 #> 511 versicolor Sepal.Width 3.2 #> 1011 virginica Sepal.Width 3.3 #> 12 setosa Petal.Length 1.4 #> 512 versicolor Petal.Length 4.7 #> 1012 virginica Petal.Length 6.0 #> 13 setosa Petal.Width 0.2 #> 513 versicolor Petal.Width 1.4 #> 1013 virginica Petal.Width 2.5
b_gather(mini_iris, key = 'flower_att', value = 'measurement', columns = 1:4)
#> Warning: the condition has length > 1 and only the first element will be used
#> Species flower_att measurement #> 1 setosa Sepal.Length 5.1 #> 51 versicolor Sepal.Length 7.0 #> 101 virginica Sepal.Length 6.3 #> 11 setosa Sepal.Width 3.5 #> 511 versicolor Sepal.Width 3.2 #> 1011 virginica Sepal.Width 3.3 #> 12 setosa Petal.Length 1.4 #> 512 versicolor Petal.Length 4.7 #> 1012 virginica Petal.Length 6.0 #> 13 setosa Petal.Width 0.2 #> 513 versicolor Petal.Width 1.4 #> 1013 virginica Petal.Width 2.5
b_gather(mini_iris, key = 'flower_att', value = 'measurement', columns = -5)
#> Species flower_att measurement #> 1 setosa Sepal.Length 5.1 #> 51 versicolor Sepal.Length 7.0 #> 101 virginica Sepal.Length 6.3 #> 11 setosa Sepal.Width 3.5 #> 511 versicolor Sepal.Width 3.2 #> 1011 virginica Sepal.Width 3.3 #> 12 setosa Petal.Length 1.4 #> 512 versicolor Petal.Length 4.7 #> 1012 virginica Petal.Length 6.0 #> 13 setosa Petal.Width 0.2 #> 513 versicolor Petal.Width 1.4 #> 1013 virginica Petal.Width 2.5
b_gather(mini_iris, key = 'flower_att', value = 'measurement', columns = '^(Sepal|Petal)',regex = TRUE)
#> Species flower_att measurement #> 1 setosa Sepal.Length 5.1 #> 51 versicolor Sepal.Length 7.0 #> 101 virginica Sepal.Length 6.3 #> 11 setosa Sepal.Width 3.5 #> 511 versicolor Sepal.Width 3.2 #> 1011 virginica Sepal.Width 3.3 #> 12 setosa Petal.Length 1.4 #> 512 versicolor Petal.Length 4.7 #> 1012 virginica Petal.Length 6.0 #> 13 setosa Petal.Width 0.2 #> 513 versicolor Petal.Width 1.4 #> 1013 virginica Petal.Width 2.5