Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrange data frame columns by class: numeric before character

Tags:

sorting

r

Good afternoon ,

Assume we have the following dataset :

dput(head(cylinder))
structure(list(X19910108 = c("19910109", "19910104", "19910104", 
"19910111", "19910104", "19910111"), X126 = c("X266", "B7", "T133", 
"J34", "T218", "X249"), TVGUIDE = c("TVGUIDE", "MODMAT", "MASSEY", 
"KMART", "MASSEY", "ROSES"), X25503 = c(25503L, 47201L, 39039L, 
37351L, 38039L, 35751L), YES = c("YES", "YES", "YES", "NO", "YES", 
"NO"), KEY = c("KEY", "KEY", "KEY", "KEY", "KEY", "KEY"), YES.1 = c("YES", 
"YES", "YES", "YES", "YES", "YES"), BENTON = c("BENTON", "BENTON", 
"BENTON", "BENTON", "BENTON", "BENTON"), GALLATIN = c("GALLATIN", 
"GALLATIN", "GALLATIN", "GALLATIN", "GALLATIN", "GALLATIN"), 
    UNCOATED = c("UNCOATED", "UNCOATED", "UNCOATED", "UNCOATED", 
    "UNCOATED", "COATED"), UNCOATED.1 = c("UNCOATED", "COATED", 
    "UNCOATED", "COATED", "UNCOATED", "COATED"), NO = c("NO", 
    "NO", "NO", "NO", "NO", "NO"), LINE = c("LINE", "LINE", "LINE", 
    "LINE", "LINE", "LINE"), YES.2 = c("YES", "YES", "YES", "YES", 
    "YES", "YES"), Motter94 = c("Motter94", "WoodHoe70", "WoodHoe70", 
    "WoodHoe70", "WoodHoe70", "Motter94"), X821 = c(821L, 815L, 
    816L, 816L, 816L, 827L), X2 = c(2, 9, 9, 2, 2, 2), TABLOID = c("TABLOID", 
    "CATALOG", "CATALOG", "TABLOID", "CATALOG", "TABLOID"), NorthUS = c("NorthUS", 
    "NorthUS", "NorthUS", NA, "NorthUS", "CANADIAN"), X1911 = c(NA, 
    NA, 1910L, 1910L, 1910L, 1911L), X55 = c(55, 62, 52, 50, 
    50, 50), X46 = c(46L, 40L, 40L, 46L, 40L, 46L), X0.2 = c("0.3", 
    "0.433", "0.3", "0.3", "0.267", "0.3"), X17 = c(15, 16, 16, 
    17, 16.8, 16.5), X78 = c(80L, 80L, 75L, 80L, 76L, 75L), X0.75 = c(0.75, 
    NA, 0.3125, 0.75, 0.4375, 0.75), X20 = c(20L, 30L, 30L, 30L, 
    28L, 30L), X13.1 = c(6.6, 6.5, 5.6, 0, 8.6, 0), X1700 = c(1900L, 
    1850L, 1467L, 2100L, 1467L, 2600L), X50.5 = c(54.9, 53.8, 
    55.6, 57.5, 53.8, 62.5), X36.4 = c(38.5, 39.8, 38.8, 42.5, 
    37.6, 37.5), X0 = c(0, 0, 0, 5, 5, 6), X0.1 = c(0, 0, 0, 
    0, 0, 0), X2.5 = c(2.5, 2.8, 2.5, 2.3, 2.5, 2.5), X1 = c(0.7, 
    0.9, 1.3, 0.6, 0.8, 0.6), X34 = c(34, 40, 40, 35, 40, 30), 
    X40 = c(40L, 40L, 40L, 40L, 40L, 40L), X105 = c(105, 103.87, 
    108.06, 106.67, 103.87, 106.67), X100 = c(100L, 100L, 100L, 
    100L, 100L, 100L), band = c("noband", "noband", "noband", 
    "noband", "noband", "noband")), row.names = c(NA, 6L), class = "data.frame")

The columns types are :

sapply(cylinder,class)

  X19910108        X126     TVGUIDE      X25503         YES         KEY       YES.1      BENTON    GALLATIN 
"character" "character" "character"   "integer" "character" "character" "character" "character" "character" 
   UNCOATED  UNCOATED.1          NO        LINE       YES.2    Motter94        X821          X2     TABLOID 
"character" "character" "character" "character" "character" "character"   "integer"   "numeric" "character" 
    NorthUS       X1911         X55         X46        X0.2         X17         X78       X0.75         X20 
"character"   "integer"   "numeric"   "integer" "character"   "numeric"   "integer"   "numeric"   "integer" 
      X13.1       X1700       X50.5       X36.4          X0        X0.1        X2.5          X1         X34 
  "numeric"   "integer"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric" 
        X40        X105        X100        band 
  "integer"   "numeric"   "integer" "character" 

I want to reorder the dataset columns such that the "numeric" columns ( numeric & integers ) are first at the left . The "character" columns must be at the right !

Thank you for help !

like image 684
Tou Mou Avatar asked Mar 26 '21 20:03

Tou Mou


2 Answers

We could also use a single where with an | logical operator that combines the two expressions with a lambda call

library(dplyr)
df %>% 
    select(where(~ is.character(.)|is.numeric(.)))
like image 119
akrun Avatar answered Jan 04 '23 21:01

akrun


Something along those lines would work

library(dplyr)
df %>% select(where(is.numeric), where(is.character))
like image 29
Marcelo Avila Avatar answered Jan 04 '23 19:01

Marcelo Avila