Home > database >  Showing unique values/levels for each character/factor variable at once?
Showing unique values/levels for each character/factor variable at once?

Time:06-09

How to show unique values/levels for each character/factor variable at once?

Data

library(tidyverse)

d = tibble(age = rnorm(10, 50, 3),
           sex = rep(c("male", "female"), 5),
           name = letters[1:10]) %>% 
  mutate(sex = as.factor(sex))

d

enter image description here

Checking variables one-by-one is time consuming as I work with large datasets

levels(d$sex)

enter image description here

CodePudding user response:

library(tidyverse)
d %>%
  select(!where(is.numeric)) %>%
  split.default(names(.)) %>%
  lapply(unique)
  
$name
name
1:    a
2:    b
3:    c
4:    d
5:    e
6:    f
7:    g
8:    h
9:    i
10:    j

$sex
sex
1:   male
2: female
  

CodePudding user response:

is this what you're looking for?

d %>% mutate_all(funs(replace(., duplicated(.), NA)))

# A tibble: 10 x 3
     age sex    name 
   <dbl> <fct>  <chr>
 1  54.0 male   a    
 2  50.3 female b    
 3  49.1 NA     c    
 4  47.0 NA     d    
 5  49.6 NA     e    
 6  51.0 NA     f    
 7  54.4 NA     g    
 8  56.9 NA     h    
 9  47.3 NA     i    
10  52.4 NA     j    

To sort the values and push NA to the last rows

d %>% 
  mutate_all(list(~replace(., duplicated(.), NA))) %>% 
  mutate_all(list(~sort(., na.last = TRUE))) %>% 
  filter(if_any(everything(), ~ !is.na(.))) # To remove rows with all NA's

CodePudding user response:

You can tweak the argument maxsum in the function summary to display as many levels as you like for each factor.

d = data.frame(age = rnorm(10, 50, 3),
           sex = rep(c("male", "female"), 5),
           name = letters[1:10])
d$sex <- as.factor(d$sex)
d$name <- as.factor(d$name)

summary(d, maxsum = 10000000)
  • Related