Home > other >  How can you simply select consecutive variables by name, as with the : operator in SAS?
How can you simply select consecutive variables by name, as with the : operator in SAS?

Time:02-01

If you are familiar with SAS, you know how the ":" operator works.

For example, I have a table (Table1) for which the column names are like this: Surface Circle Area1 Area2 Area3 ... Area12

I can call the Area1 Area2 ... Area12 columns like this:

data Table2;
set Table1;
keep Area:;
run;

What is the equivalent of this in R ?

Imagine the following dataset:

a1 = 1:10
a2 = 11:20
a3 = 21:30
a4 = 31:40
b = 50:59
df = data.frame(a1, a2, a3, a4, b)

How can I select the columns a1 ... a4 with a command as simple as the one we use in SAS?

CodePudding user response:

If I understood correctly what you are asking, your are looking for something like this (which makes use of dplyr package):

df %>% select(a1:a4)

Edit: If instead you are looking for a way to select all columns which start with the pattern 'a', you need the starts_with helper:

df %>% select(starts_with("a"))

Link to the documentation

CodePudding user response:

You can do this in base R with subset:

subset(df, select = a1:a4)
  •  Tags:  
  • Related