dfFactor: Get a factor vector for a data.frame

Description Usage Arguments Details Value Author(s) Examples

View source: R/dfmat.R

Description

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

Usage

1
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.

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.

Value

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

Author(s)

Jitao David Zhang <jitao_david.zhang@roche.com>

Examples

1
2
3
4
5
6
7
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)

ribiosUtils documentation built on March 13, 2020, 2:54 a.m.