Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R equivalent of Stata *

Tags:

r

stata

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?

like image 382
bill999 Avatar asked Dec 06 '22 22:12

bill999


1 Answers

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
like image 189
A5C1D2H2I1M1N2O1R2T1 Avatar answered Dec 20 '22 13:12

A5C1D2H2I1M1N2O1R2T1