Home > Mobile >  collapse R package: rearraning or reposition columns in custom order
collapse R package: rearraning or reposition columns in custom order

Time:02-13

I'm using collapse R package want to reposition columns. In tidyverse or dplyr, this can be achieved by using everything() function. However everything() function is not working with fselect function from collapse. Any thoughts.

library(collapse)
library(tidyverse)

wlddev %>%
  qDT() %>% 
  fselect(date, year, decade)

             date year decade
    1: 1961-01-01 1960   1960
    2: 1962-01-01 1961   1960
    3: 1963-01-01 1962   1960
    4: 1964-01-01 1963   1960
    5: 1965-01-01 1964   1960
   ---                       
13172: 2017-01-01 2016   2010
13173: 2018-01-01 2017   2010
13174: 2019-01-01 2018   2010
13175: 2020-01-01 2019   2010
13176: 2021-01-01 2020   2020

wlddev %>%
  qDT() %>% 
  fselect(date, year, decade, everything())


Error in `peek_vars()`:
! `everything()` must be used within a *selecting* function.
i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.
Run `rlang::last_error()` to see where the error occurred.

CodePudding user response:

With collapse, there is colorder where we can specify the pos

colorder(X, ..., pos = c("front", "end", "exchange"))

library(collapse)
library(dplyr)
wlddev %>% 
   qDT %>% 
   colorder(date, year, decade, pos = "front") 

-output

            date  year decade     country  iso3c             region              income   OECD    PCGDP LIFEEX  GINI       ODA      POP
           <Date> <int>  <int>      <char> <fctr>             <fctr>              <fctr> <lgcl>    <num>  <num> <num>     <num>    <num>
    1: 1961-01-01  1960   1960 Afghanistan    AFG         South Asia          Low income  FALSE       NA 32.446    NA 116769997  8996973
    2: 1962-01-01  1961   1960 Afghanistan    AFG         South Asia          Low income  FALSE       NA 32.962    NA 232080002  9169410
    3: 1963-01-01  1962   1960 Afghanistan    AFG         South Asia          Low income  FALSE       NA 33.471    NA 112839996  9351441
    4: 1964-01-01  1963   1960 Afghanistan    AFG         South Asia          Low income  FALSE       NA 33.971    NA 237720001  9543205
    5: 1965-01-01  1964   1960 Afghanistan    AFG         South Asia          Low income  FALSE       NA 34.463    NA 295920013  9744781
   ---                                                                                                                                  
13172: 2017-01-01  2016   2010    Zimbabwe    ZWE Sub-Saharan Africa Lower middle income  FALSE 1224.310 60.294    NA 687659973 14030390
13173: 2018-01-01  2017   2010    Zimbabwe    ZWE Sub-Saharan Africa Lower middle income  FALSE 1263.321 60.812  44.3 753909973 14236745
13174: 2019-01-01  2018   2010    Zimbabwe    ZWE Sub-Saharan Africa Lower middle income  FALSE 1305.783 61.195    NA 794510010 14439018
13175: 2020-01-01  2019   2010    Zimbabwe    ZWE Sub-Saharan Africa Lower middle income  FALSE 1183.099 61.490  50.3 988039978 14645468
13176: 2021-01-01  2020   2020    Zimbabwe    ZWE Sub-Saharan Africa Lower middle income  FALSE       NA     NA    NA       

CodePudding user response:

setcolorder is the equivalent of everything() in Data Tables.

wlddev %>%
  qDT() %>% 
  setcolorder(c("date", "year", "decade")) %>% eval()
  • Related