reshape_longer() reshapes one or more columns from wide into long format.

reshape_longer(
  x,
  columns = colnames(x),
  names.to = "key",
  values.to = "value",
  labels = NULL,
  numeric.timevar = FALSE,
  id = ".id"
)

Arguments

x

A data frame.

columns

Names of variables (as character vector), or column index of variables, that should be reshaped. If multiple column groups should be reshaped, use a list of vectors (see 'Examples').

names.to

Character vector with name(s) of key column(s) to create in output. Either one name per column group that should be gathered, or a single string. In the latter case, this name will be used as key column, and only one key column is created.

values.to

Character vector with names of value columns (variable names) to create in output. Must be of same length as number of column groups that should be gathered. See 'Examples'.

labels

Character vector of same length as values.to with variable labels for the new variables created from gathered columns. See 'Examples'.

numeric.timevar

Logical, if TRUE, the values of the names.to column will be recoded to numeric values, in sequential ascending order.

id

Name of ID-variable.

Value

A reshaped data frame.

See also

Examples

# Reshape one column group into long format
mydat <- data.frame(
  age = c(20, 30, 40),
  sex = c("Female", "Male", "Male"),
  score_t1 = c(30, 35, 32),
  score_t2 = c(33, 34, 37),
  score_t3 = c(36, 35, 38)
)

reshape_longer(
  mydat,
  columns = c("score_t1", "score_t2", "score_t3"),
  names.to = "time",
  values.to = "score"
)
#>   age    sex     time score .id
#> 1  20 Female score_t1    30   1
#> 2  30   Male score_t1    35   2
#> 3  40   Male score_t1    32   3
#> 4  20 Female score_t2    33   1
#> 5  30   Male score_t2    34   2
#> 6  40   Male score_t2    37   3
#> 7  20 Female score_t3    36   1
#> 8  30   Male score_t3    35   2
#> 9  40   Male score_t3    38   3


# Reshape multiple column groups into long format
mydat <- data.frame(
  age = c(20, 30, 40),
  sex = c("Female", "Male", "Male"),
  score_t1 = c(30, 35, 32),
  score_t2 = c(33, 34, 37),
  score_t3 = c(36, 35, 38),
  speed_t1 = c(2, 3, 1),
  speed_t2 = c(3, 4, 5),
  speed_t3 = c(1, 8, 6)
)

reshape_longer(
  mydat,
  columns = list(
    c("score_t1", "score_t2", "score_t3"),
    c("speed_t1", "speed_t2", "speed_t3")
  ),
  names.to = "time",
  values.to = c("score", "speed")
)
#>   age    sex     time score speed .id
#> 1  20 Female score_t1    30     2   1
#> 2  30   Male score_t1    35     3   2
#> 3  40   Male score_t1    32     1   3
#> 4  20 Female score_t2    33     3   1
#> 5  30   Male score_t2    34     4   2
#> 6  40   Male score_t2    37     5   3
#> 7  20 Female score_t3    36     1   1
#> 8  30   Male score_t3    35     8   2
#> 9  40   Male score_t3    38     6   3

# or ...
reshape_longer(
  mydat,
  list(3:5, 6:8),
  names.to = "time",
  values.to = c("score", "speed")
)
#>   age    sex     time score speed .id
#> 1  20 Female score_t1    30     2   1
#> 2  30   Male score_t1    35     3   2
#> 3  40   Male score_t1    32     1   3
#> 4  20 Female score_t2    33     3   1
#> 5  30   Male score_t2    34     4   2
#> 6  40   Male score_t2    37     5   3
#> 7  20 Female score_t3    36     1   1
#> 8  30   Male score_t3    35     8   2
#> 9  40   Male score_t3    38     6   3

# gather multiple columns, label columns
x <- reshape_longer(
  mydat,
  list(3:5, 6:8),
  names.to = "time",
  values.to = c("score", "speed"),
  labels = c("Test Score", "Time needed to finish")
)

library(sjlabelled)
str(x$score)
#>  num [1:9] 30 35 32 33 34 37 36 35 38
#>  - attr(*, "label")= chr "Test Score"
get_label(x$speed)
#> [1] "Time needed to finish"