dichotomize | R Documentation |
Converts a matrix or data frame into a dichotomized data frame. Where each possible category is assigned a 1 or a 0.
dichotomize(x, id = NULL, categories = NULL, NAasNA = TRUE, sortbyItem = FALSE)
x |
a data frame or matrix. |
id |
a vector of unique values indicating the ids of the cases.
If |
categories |
a vector indicating all possible categories.
If |
NAasNA |
a logical value indicating if NAs should be kept
as NAs. If |
sortbyItem |
a logical value indicating if the resulting data frame
should be order by item names, on the contrary, by ids.
Default is |
A data frame with columns for the ids, the item names and all the possible item categories, where 1s mean the person answered that category for that item, and 0s mean the contrary.
data(dichodata)
# Dichotomize with all present categories
ex1 <- dichotomize(dichodata, categories = NULL)
head(ex1)
# Dichotomize with fixed set of categories
ex2 <- dichotomize(dichodata, categories = c('A','B','C','D'))
head(ex2)
# Dichotomize with NAs as 0s
ex3 <- dichotomize(dichodata, NAasNA = FALSE)
head(ex3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.