Learn R Programming

ribiosUtils (version 1.5-6)

dfFactor: Get a factor vector for a data.frame

Description

The function try to assign a factor vector for a data.frame object. See details below.

Usage

dfFactor(df, sample.group)

Arguments

df

A data.frame

sample.group

A character, number or a vector of factors, from which the factor vector should be deciphered. See details below.

Value

A factor vector with the same length as the data.frame

Details

The function tries to get a factor vector of the same length as the number of rows in the data.frame. The determination is done in the following order: Step 1: It tries to find a column in the data.frame with the name as given by sample.group. If found, this column is transformed into a factor if not and returned. Step 2: It tries to interpret the sample.group as an integer, as the index of the column in the data.frame giving the factor. Step 3: When sample.group itself is a vector of the same length as the data.frame, it is cast to factor when it is still not and returned.

Otherwise the program stops with error.

Examples

Run this code
# NOT RUN {
df <- data.frame(gender=c("M", "M", "F", "F", "M"),
age=c(12,12,14,12,14), score=c("A", "B-", "C", "B-", "A"))
dfFactor(df, "gender")
dfFactor(df, "score")
dfFactor(df, 1L)
dfFactor(df, 2L)
dfFactor(df, df$score)

# }

Run the code above in your browser using DataLab