Daniel Cho
Daniel Cho

Reputation: 827

How do you make vector of variables with sequenced name in R?

I'm trying to find an easier way to make a vector of sequenced variable name.

for example, there are many variables in the data, and I want to select h190361, h190362, h190363, h190364, h190365 from the data.

in SAS or STATA or SPSS, if you want to pick some sequenced variables, you can simply write 'h190361-h190365' or 'from h190361 to h190365'

But I don't know any simple syntax for R.

hard way will be to write all the variable names,

    x <- c(df$h190361, df$h190362, df$h190363, df$h190364, df$h190365)

but if there are many variables, it will be too much working.

another way that I thought of is to use 'paste' syntax,

    k <- paste("h190", 361:365, sep = "")
    x <- df[,k]

which returns desired result.

however, this seems not natural and not simple as SAS, SPSS, or STATA.

is there more easier way or simple syntax to select sequenced variables in R?

Thank you.

Upvotes: 0

Views: 57

Answers (2)

Łukasz Deryło
Łukasz Deryło

Reputation: 1860

Maybe select from dplyr package?

select(df, h190361:h190365)

or with pipe:

df %>% select(h190361:h190365)

But be careful! select(df, X:Y) means "take columns X and Y from df plus everything in between", so if you have some columns of names, say, X, Y, Z between h190361 and h190365, they would be included too.

Upvotes: 1

chrk623
chrk623

Reputation: 76

If you can find which columns you want to subset out easily, then you can just do something like..

df2 <- df[,1:4]

However, this approach would only work on sequenced columns.

Another approach would be to use regular expression.

df2 <- df[,grep("h190",colnames(df))]

You can change the pattern in grep() to suit ur needs.

Upvotes: 0

Related Questions