View source: R/all_custom_functions.R
prevTabl | R Documentation |
This function creates a prevalence table for a tested condition in a data table. One can define a function which returns a logic output for each element in the data. The function then counts how many cases have returned TRUE for each column and presents this information. In addition, one can define a categorical variable and return the results for each level separately. The function can deal with missing values in the data.
prevTabl(X, FUN, catVar = NULL, atLeastOnce = FALSE)
X |
A data table, classically a data frame. |
FUN |
A function to test a specific condition along each column of the data. Must return a logical value for each element (or NA). |
catVar |
A factor with the same length as there are rows in the data. When supplied, the prevalences of successes will be presented for each level of catVar separately. |
atLeastOnce |
A logical value. If set to TRUE, the final table will include an additional column, which will present whether a specific case scored at least one TRUE over all columns of the data. |
The (silent) return object comprises the number of successes, the number of observations and the successrate for each column. If atLeastOnce was set to TRUE, then the same information is additionally returned for this extra column. The final prevalence table is printed when the function is called and also stored in the return object.
f <- function(x){x < 3.1}
res <- prevTabl(X = iris[,-5], FUN = f, catVar = iris$Species, atLeastOnce = TRUE)
res
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.