longToWide: Turn data in long format to wide format

View source: R/all_custom_functions.R

longToWideR Documentation

Turn data in long format to wide format

Description

A function to turn long format data to wide format data. Can deal with multiple repeatedly measured variables and fixed variables of different types. An observation-ID variable (e.g. subject names), a repeated-measures indicator variable (e.g. "Timepoint") and the names of the repeatedly measured variables have to be provided. The function will give specific warnings in case there are peculiar patterns in the data (e.g. different values of a presumably fixed variable in an ID).

Usage

longToWide(x, IDvar, repColnm, repVars, colNmStr = '', verbose.mssg = TRUE,
verbose.warn = TRUE)

Arguments

x

A data frame containing the data in long format

IDvar

A string with the name of the observation ID variable (e.g. subject names)

repColnm

The name of the column in the long format data frame indicating the repeated measurement occasions.

repVars

A vector including the variable names of the variables with repeated measurements

colNmStr

A string which is put into the colnames of the repVars, separating their colnames from the repColnm levels.

verbose.mssg

Logical indicating whether messages should be reported.

verbose.warn

Logical indicating whether warnings should be reported.

Value

The transformed data frame in wide format.

Examples

### Generate artificial data:
d <- data.frame(matrix(1:24, nrow = 3, byrow = TRUE))
colnames(d) <- c('fix', 'S','T1_A', 'T2_A', 'T3_A', 'T1_B', 'T2_B', 'T3_B')
d$S <- paste0('S', 1:3)
dat <- wideToLong(x = d, nRep = 3, repColnm = 'timepoint')
dat$S <- as.factor(dat$S)
dat$B <- factor(dat$B, labels = c('a', 'b','c', 'd', 'e', 'f', 'g', 'h', 'j'))
### Apply function:
longToWide(x = dat, IDvar = 'S', repColnm = 'timepoint', repVars = c('B','A'))

ryannick28/CustomFunctionsYrotha documentation built on June 1, 2025, 4:02 p.m.