一次显示每个字符/因子变量的唯一值/级别?

发布于 2025-02-05 17:34:16 字数 538 浏览 2 评论 0原文

如何一次显示每个字符/因子变量的唯一值/级别?

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

levels(d$sex)

​sstatic.net/tmjrn.png“ rel =“ nofollow noreferrer”> 1 “女性”“男性”“男性”

预期的结果应该是这样的

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)

1 "female" "male"

Expected result should be something like this

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年华零落成诗 2025-02-12 17:34:16
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
  
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
  
三生殊途 2025-02-12 17:34:16

这是您想要的吗?

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    

对值进行排序并将NA推到最后一行

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

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
べ映画 2025-02-12 17:34:16

您可以在函数摘要中调整参数最大值,以显示每个因素所需的多个级别。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文