Select multiple variables while writing a code

Hello
I was wondering if there is a way to select multiple variables while writing a code. I mean when we type the dataframe name followed by $, we get the list of all variables but it only allows selecting one at a time.

Is there a way (like a package or rstudio add in) that allows selecting multiple?

what I have been doing so far is (and I got the ideas from this 2012 post ):

  • use dput(names(dataframe)) then select what I need
  • use starts_with, ends_with, contains, etc... from dplyr (but these only work whenever there is a unique pattern to the variables of interest).

with all these nice rstudio add ins around, I was hoping there is a nicer/ more efficient way to select the variables without having to type them individually.

this sounds like a basic question and I apologize in advance if it has been asked before but I could not find an answer here or on stackoverflow.

thanks a lot

Hi,
You can use squared brackets to select rows and columns of a data frame, here are some examples:

# sample data'
mydf<-head(mtcars)
mydf
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
# Use numeric indices subset columns 
mydf[,c(1,3,6)]
#>                    mpg disp    wt
#> Mazda RX4         21.0  160 2.620
#> Mazda RX4 Wag     21.0  160 2.875
#> Datsun 710        22.8  108 2.320
#> Hornet 4 Drive    21.4  258 3.215
#> Hornet Sportabout 18.7  360 3.440
#> Valiant           18.1  225 3.460

# Use the column names to select columnns 
mydf[,c("mpg","disp","wt")]
#>                    mpg disp    wt
#> Mazda RX4         21.0  160 2.620
#> Mazda RX4 Wag     21.0  160 2.875
#> Datsun 710        22.8  108 2.320
#> Hornet 4 Drive    21.4  258 3.215
#> Hornet Sportabout 18.7  360 3.440
#> Valiant           18.1  225 3.460

# Use a logical vector to subset a column
mydf[,c(T,F,T,F,F,T,F,F,F,F,F)]
#>                    mpg disp    wt
#> Mazda RX4         21.0  160 2.620
#> Mazda RX4 Wag     21.0  160 2.875
#> Datsun 710        22.8  108 2.320
#> Hornet 4 Drive    21.4  258 3.215
#> Hornet Sportabout 18.7  360 3.440
#> Valiant           18.1  225 3.460

Created on 2021-01-02 by the reprex package (v0.3.0)
To select rows, you do the same before the comma.

Another very popular option to select columns is dplyr::select():

suppressPackageStartupMessages(
  library(dplyr)
)

# sample data'
mydf<-head(mtcars)
mydf
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# select columns
mydf%>%select(mpg,disp,drat)
#>                    mpg disp drat
#> Mazda RX4         21.0  160 3.90
#> Mazda RX4 Wag     21.0  160 3.90
#> Datsun 710        22.8  108 3.85
#> Hornet 4 Drive    21.4  258 3.08
#> Hornet Sportabout 18.7  360 3.15
#> Valiant           18.1  225 2.76

Created on 2021-01-02 by the reprex package (v0.3.0)

Does this answer your question?

I appreciate your help. These tips are very useful to me.
I was also asking if there is a GUI option / rstudio add in that can help with that?

Oh, I see. I must have misunderstood your initial question. In the stackoverflow link you have posted, they are suggesting the select.list() function, isn't that what you want?
I suppose you could use it like so:

mycols<-select.list(names(mtcars),multiple=TRUE,
                    graphics=TRUE)

mtcars[,mycols]

thanks a lot. I believe this is the best available option so far.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.