dichotomize: Dichotomize data

View source: R/dichotomize.R

dichotomizeR Documentation

Dichotomize data

Description

Converts a matrix or data frame into a dichotomized data frame. Where each possible category is assigned a 1 or a 0.

Usage

dichotomize(x, id = NULL, categories = NULL, NAasNA = TRUE, sortbyItem = FALSE)

Arguments

x

a data frame or matrix.

id

a vector of unique values indicating the ids of the cases. If NULL, an id will be created following id <- 1L:nrow(x). Default is NULL.

categories

a vector indicating all possible categories. If NULL, this vector will be created with all the non-NA values present in x. Default is NULL.

NAasNA

a logical value indicating if NAs should be kept as NAs. If FALSE, NAs will be 0s. Default is TRUE.

sortbyItem

a logical value indicating if the resulting data frame should be order by item names, on the contrary, by ids. Default is FALSE.

Value

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.

Examples

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)



classicaltest documentation built on Oct. 28, 2024, 5:06 p.m.