R equivalent of Stata *
Asked Answered
R

2

5

In Stata, if I have these variables: var1, var2, var3, var4, var5, and var6, I can select all of them with the command var*. Does R have a similar functionality?

Runge answered 4/3, 2015 at 2:26 Comment(1)
Strictly speaking, var* is not a command, but a varlist (a list of variable names) that uses a special character or wildcard.Joiejoin
N
7

You can grep to do this kind of regexp matching among the column names:

x = c(1, 2, 3)
df = data.frame(var1=x, var2=x, var3=x, other=x)
df[, grep("var*", colnames(df))]

Output:

  var1 var2 var3
1    1    1    1
2    2    2    2
3    3    3    3

So, basically just making use of the usual df[rows_to_keep, columns_to_keep] indexing syntax, and feeding in the results of grep as the columns_to_keep.

Noland answered 4/3, 2015 at 2:34 Comment(2)
@Runge To be safe (and to match the Stata query), you'd want to use "^var*" to make sure that the varname starts with "var". Try grep("var*",c("revarnish"))Nikethamide
@thelatemail Yeah, you're right. The * isn't needed. Ah, the comment disappeared, but: "^var" is sufficient; there's no need to finish with *.Nikethamide
I
12

The select function from the "dplyr" package offers several flexible ways to select variables. For instance, using @Marius's sample data, try the following:

library(dplyr)
df %>% select(starts_with("var"))        # At the start
df %>% select(num_range("var", 1:3))     # specifying range
df %>% select(num_range("var", c(1, 3))) # gaps are allowed
Inverse answered 4/3, 2015 at 2:41 Comment(0)
N
7

You can grep to do this kind of regexp matching among the column names:

x = c(1, 2, 3)
df = data.frame(var1=x, var2=x, var3=x, other=x)
df[, grep("var*", colnames(df))]

Output:

  var1 var2 var3
1    1    1    1
2    2    2    2
3    3    3    3

So, basically just making use of the usual df[rows_to_keep, columns_to_keep] indexing syntax, and feeding in the results of grep as the columns_to_keep.

Noland answered 4/3, 2015 at 2:34 Comment(2)
@Runge To be safe (and to match the Stata query), you'd want to use "^var*" to make sure that the varname starts with "var". Try grep("var*",c("revarnish"))Nikethamide
@thelatemail Yeah, you're right. The * isn't needed. Ah, the comment disappeared, but: "^var" is sufficient; there's no need to finish with *.Nikethamide

© 2022 - 2024 — McMap. All rights reserved.