collapse R 包:按自定义顺序重新排列或重新定位列

collapse R package: rearraning or reposition columns in custom order

我正在使用 collapse R 包想要重新定位列。在tidyversedplyr 中,这可以通过使用everything() 函数来实现。但是 everything() 函数无法与 collapse 中的 fselect 函数一起使用。有什么想法。

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.

setcolorder 相当于数据表中的 everything()

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

有了collapse,就有了colorder,我们可以在其中指定pos

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

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

-输出

            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