dplyr - 如何选择某种类型的列

pal*_*czy 19 r dplyr

有没有简洁的方法来选择特定类型的列dplyr?例如,如何选择dplyr链中的所有字符列?

Ric*_*ard 36

Dplyr 0.5具有select_if()允许你写的select_if(is.character)

  • 这使代码比在dplyr链中使用apply更清晰! (2认同)

Kou*_*ndy 12

你可以这样做

dt %>% select(which(sapply(.,is.character)))
Run Code Online (Sandbox Code Playgroud)


Lev*_*ley 11

dplyr1.0.0 开始,*_if功能和好友已被取代。现在建议使用 选择助手 wherefrom tidyselect

https://dplyr.tidyverse.org/reference/select.html
https://tidyselect.r-lib.org/reference/where.html

library(dplyr)

starwars %>% 
    select(where(is.character))
#> # A tibble: 87 x 8
#>    name        hair_color   skin_color eye_color sex   gender  homeworld species
#>    <chr>       <chr>        <chr>      <chr>     <chr> <chr>   <chr>     <chr>  
#>  1 Luke Skywa~ blond        fair       blue      male  mascul~ Tatooine  Human  
#>  2 C-3PO       <NA>         gold       yellow    none  mascul~ Tatooine  Droid  
#>  3 R2-D2       <NA>         white, bl~ red       none  mascul~ Naboo     Droid  
#>  4 Darth Vader none         white      yellow    male  mascul~ Tatooine  Human  
#>  5 Leia Organa brown        light      brown     fema~ femini~ Alderaan  Human  
#>  6 Owen Lars   brown, grey  light      blue      male  mascul~ Tatooine  Human  
#>  7 Beru White~ brown        light      blue      fema~ femini~ Tatooine  Human  
#>  8 R5-D4       <NA>         white, red red       none  mascul~ Tatooine  Droid  
#>  9 Biggs Dark~ black        light      brown     male  mascul~ Tatooine  Human  
#> 10 Obi-Wan Ke~ auburn, whi~ fair       blue-gray male  mascul~ Stewjon   Human  
#> # ... with 77 more rows
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.3.0)于 2020 年 6 月 2 日创建


Tal*_*evy 5

一种方法是首先获得不同列的类.所以假设我们有一些数据:

library(dplyr)
DT <- data.frame(A = letters[1:6], B = c(T,F,F), C = seq(1,2,length.out = 6), D = 1:6)
dt <- tbl_df(DT)
dt$A <- as.character(dt$A)
Run Code Online (Sandbox Code Playgroud) 产量
       A     B     C     D
  (chr) (lgl) (dbl) (int)
1      a  TRUE   1.0     1
2      b FALSE   1.2     2
3      c FALSE   1.4     3
4      d  TRUE   1.6     4
5      e FALSE   1.8     5
6      f FALSE   2.0     6
Run Code Online (Sandbox Code Playgroud)

我们现在可以使用which函数获取类:

cls <- sapply(dt, class)
cls
Run Code Online (Sandbox Code Playgroud) 产量
        A         B         C         D 
 "character" "logical" "numeric" "integer" 
Run Code Online (Sandbox Code Playgroud)

现在很简单:

newDF <- dt %>% select(which(cls=="character"))
newDF
Run Code Online (Sandbox Code Playgroud) 产量
      A
  (chr)
1     a
2     b
3     c
4     d
5     e
6     f
Run Code Online (Sandbox Code Playgroud)