aggreg: Aggregate Data to the Subject Level

View source: R/aggreg.r

aggregR Documentation

Aggregate Data to the Subject Level

Description

Function that aggregates a dataset to the subject level.

Usage

aggreg(data, id, vars, grep=FALSE, na.rm=TRUE)

Arguments

data

data frame to aggregate.

id

argument to specify a subject id variable.

vars

optional character vector (giving the names of the variables to aggregate) or a numeric vector (giving the position of the columns in the data frame corresponding to the variables).

grep

logical indicating whether variable names should be matched using grep (default is FALSE).

na.rm

logical indicating whether missing values should be removed before aggregating the variables (default is TRUE).

Details

The function aggregates a dataset in the long format to the subject level. For numeric, integer, and logical variables, the subject-level means are computed. For factors and character variables, the first (non-missing) value is returned.

Value

A data frame.

Author(s)

Wolfgang Viechtbauer wvb@wvbauer.com

Examples

# illustrative dataset
dat <- data.frame(subj=rep(1:4, each=5),
                  sex = rep(c("male", "female"), each=2*5),
                  obs = 1:5,
                  age = rep(c(20,31,27,22), each=5),
                  stress = c(2,3,NA,4,2, 3,3,NA,3,NA, 1,1,2,6,4, 1,2,1,3,1))
dat

# aggregate the dataset
aggreg(dat, subj)

# aggregate the dataset for selected variables
aggreg(dat, subj, vars=c("subj","stress"))

# aggregate the dataset for selected variables
aggreg(dat, subj, vars=1:2)

wviechtb/esmpack documentation built on April 12, 2025, 9:48 p.m.