withinFun: withinFun, applying functions on clustered data structure.

Description Usage Arguments Details Value Examples

Description

The function applies a function on a nested data structure. The function applied can be any type of function with one argument. The function uses the function map and the package stringr. For how to enter the function, see details.

Usage

1
2
withinFun(data, id.var, fun, return.data.frame = FALSE, var.spec = NULL,
  fun.spec = NULL)

Arguments

data

An object, a data frame or a list.

id.var

The variable in which the scores are clustered, usually an ID variable. Must be a character string.

fun

The function to apply on the scores within the ID variable. Must be entered as a character string. See details for further information.

return.data.frame

Logical, should a data frame be returned, matching the results as a variable on the same level as the ID variable? Default is to FALSE.

var.spec

Not implemented yet. If a variable cannot be detected, it can be specified here. Default is to NULL.

fun.spec

Not implemented yet. If a function cannot be detected, it can be specified here. Default is to NULL.

Details

Entering a function. The argument fun needs to be a character string. It needs to have the following struncture fun = "function(variable), arguments to function".

The results can be either of the following, depending on return.data.frame

Value

Returns a data frame or a list with the results of the function, fun. Can be either a list or a data frame, depending on the value of return.data.frame.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## make sample data frame
variable_1 <- rnorm(100, 1)
variable_2 <- rnorm(100, 2)
id <- sample(letters[1:5], 100, replace = TRUE)
data <- data.frame(id, variable_1, variable_2)

## run function, get length of each ID variable on "variable1", returning a data frame
withinFun(data, id.var="id", "length(variable1)", return.data.frame = TRUE)

# get the mean of each ID variable on "variable1", returning a list with the entries only
withinFun(data, id.var="id", "mean(variable1, na.rm =TRUE)")

kthorstmann/horst documentation built on May 20, 2019, 7:04 p.m.