Description Usage Arguments Details Value Examples
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.
1 2 |
data |
An object, a |
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 |
var.spec |
Not implemented yet. If a variable cannot be detected, it can be specified here. Default is to |
fun.spec |
Not implemented yet. If a function cannot be detected, it can be specified here. Default is to |
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"
.
variable
the function to apply, such as mean
, length
, sd
.
function
the variable to apply the function on. This variable must not be enterd as a string, just as plain text.
further arguments to function
further arguments that are passed to the function, such as na.rm = TRUE
for mean
.
The results can be either of the following, depending on return.data.frame
return.data.frame = TRUE
This returns the complete data frame with the new variable included. The results of the function and the input data are merged by the id.var
.The new variable has the name variable_function
.
return.data.frame = FALSE
This returns a list with each entry being an id.var
. This result can be easily used for further analyses.
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
.
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)")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.