arrange some columns by name and then include all the others

134 Views Asked by At

I am working with wide dataframes that I am popping in and out off frequently with View() in RStudio. Most of my attention is on a few columns to the left but I constantly need to scroll to columns out of frame on the far right. Is there a 'quick and diRty' way to arrange the order of some of the columns 'by name' and trail the rest?

I do not want to order the columns by position as I may need to go back a step or two and include more columns and then the positions may be pointing to the wrong columns. I do not want to subset as I never know what columns to the right that I may need to visually inspect.

My real data is around 40 columns wide; i have included a small dummy set to illustrate....

library(qdap)
library(dplyr)
arranger = data.frame(col1 = qcv(tom, dick, harry),
                  col2 = qcv(man, woman, child),
                  col3 = qcv(tall, small, middle),
                  col4 = qcv(blond, red, fair),
                  col5 = qcv(africa, europe, moon),
                  col6 = qcv(dead, alive, zombie),
                  col7 = qcv(funny, boring, sad),
                  stringsAsFactors = F)

As an example I would like to put col3 and col5 as the first two columns and then trail the rest. I am looking for a quick approach something along the line of select() from dplyr...

arranger = select(arranger, col3, col5, 'then the other columns')

Anyone have any good ideas please? TIA

1

There are 1 best solutions below

1
On BEST ANSWER

If you'd like to use the dplyr package as mentioned, then everything() will achieve this.

Here's some updated code, using your example:

library(qdap)
library(dplyr)

arranger <-  data.frame(col1 = qcv(tom, dick, harry),
                      col2 = qcv(man, woman, child),
                      col3 = qcv(tall, small, middle),
                      col4 = qcv(blond, red, fair),
                      col5 = qcv(africa, europe, moon),
                      col6 = qcv(dead, alive, zombie),
                      col7 = qcv(funny, boring, sad),
                      stringsAsFactors = F)

arrangerNew <- dplyr::select(arranger, col3, col5, everything())