Search
 
SCRIPT & CODE EXAMPLE
 

R

detect factors in r

make_factors <- function(data, max_levels=15) {
    # convert all columns in <data> that are not already factors
    # and that have fewer than <max_levels> distinct values into factors. 
    # If the column is numeric, it becomes an ordered factor.

    stopifnot(is.data.frame(data))
    for(n in names(data)){
        if(!is.factor(data[[n]]) && 
                length(unique(data[[n]])) <= max_levels) {
            data[[n]] <- if(!is.numeric(data[[n]])){
                 as.factor(data[[n]])
            } else {
                 ordered(data[[n]])
            }    
        }
    }
    data
}


# create dataset with one numeric column <foo> with few  distinct entries 
# and one character column <baz> with few  distinct entries :
data <- iris
data <- within(data, {
     foo <- round(iris[, 1])
     baz <- as.character(foo)
})   


table(data$foo)

## 4  5  6  7  8 
## 5 47 68 24  6 

str(data)

## 'data.frame':    150 obs. of  7 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ baz         : chr  "5" "5" "5" "5" ...
##  $ foo         : num  5 5 5 5 5 5 5 5 4 5 ...

str(make_factors(data))

## 'data.frame':    150 obs. of  7 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ baz         : Factor w/ 5 levels "4","5","6","7",..: 2 2 2 2 2 2 2 2 1 2 ...
##  $ foo         : Ord.factor w/ 5 levels "4"<"5"<"6"<"7"<..: 2 2 2 2 2 2 2 2 1 2 ...
Comment

PREVIOUS NEXT
Code Example
R :: R create sequence of date each quarters 
R :: R: foreach multiple argument 
R :: logical vector passed in R 
R :: logistic distribution CDF in r 
R :: r yardstick confusion matrix 
R :: no redundant combination expand grid 
R :: cbind vectors of different lengths r 
R :: extract df from lm in r 
R :: required in r 
R :: how to exclude inf in r 
R :: diff division R 
R :: vector with real numbers R 
R :: ways to examine model in r 
R :: R view storage size of variable 
Rust :: rustlang error: linker `link.exe` not found 
Rust :: how to cahce clean cargo 
Rust :: rust check if key in hashmap 
Rust :: create empty string rust 
Rust :: rust struct 
Rust :: rust lang function is never used: rustc(dead_code) 
Rust :: Project Euler #1 Multiples of 3 or 5 
Rust :: armanriazi•rust•error•[E0507]: cannot move out of `self.step` which is behind a mutable reference self.curr += self.step; 
Rust :: rust•armanriazi•iterator•index•avoid 
Rust :: rust•armanriazi•error•value used here after move 
Rust :: rust get items in a list 
Rust :: rust how to make print happen before asking for input 
Lua :: luau how to make rainbow part 
Lua :: user input lua 
Lua :: lua drawinrect 
Lua :: roblox wait for character 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =