multi.spread: Spread Multiple Variables

View source: R/dthelper-data-wrangling.R

multi.spreadR Documentation

Spread Multiple Variables

Description

Wrangles data from long to wide form

Usage

multi.spread(df, id, key, var = NULL, sep = "_", custom.append = NULL)

Arguments

df

data.frame object

id

Identifier variable. Input as string

key

Key variable to spread on. Input as string

var

Variables being spread. Defaults to all other variables (apart from id and key)

sep

Separator that appends to variable names after being spread

custom.append

Customised labels that are appended to variable names. Length should match number of unique keys

Value

data.frame object

Examples

df <- data.frame(
      id = c(1,1,1,2,2,2,3,3,3),
      time = c(1,2,3,1,2,3,1,2,3),
      v1 = 1:9,
      v2 = 11:19,
      v3 = paste0("a",1:9)
      )
df.wide <- multi.spread(df, id = "id", key = "time") 
df.wide <- multi.spread(df, id = "id", key = "time", custom.append = c("t1","t2","t3")) 
df.wide <- multi.spread(df, id = "id", key = "time", var = c("v1","v2")) 

dennisteowh/dthelper documentation built on March 19, 2022, 11:42 a.m.