0

I have a concatenated string having column names in it:

col_names = paste(c("Age", "Sex", "City", "Name"), collapse=', ')
col_names
[1] "Age, Sex, City, Name"

I also have a data frame=df that has 100s of variables. How can I select the columns present in my string (col_names) from the df using col_names?

1
  • df[names(read.csv(text = col_names))] would work. Commented Jan 13, 2016 at 17:51

3 Answers 3

1

We can use scan to get the vector of names, remove the trailing/leading spaces with trimws and use that to subset the dataset.

df[trimws(scan(text=col_names, sep=",", what=''))]

Or another option is str_extract

library(stringr)
df[str_extract_all(col_names, '\\w+')[[1]]]

data

df <- data.frame(Age= c(5, 3, 25), Sex = c("M", "F", 
 "M"), Value = 1:3, City= c("Brookings",
 "Mumbai", "Paris"), Name = LETTERS[1:3])
Sign up to request clarification or add additional context in comments.

Comments

1

Does using the strsplit function help? It will split your string into a character vector by some sort of separator. Here I use the function to subset a data frame (courtesy of akrun) by the variables in the string.

df <- data.frame(Age= c(5, 3, 25), Sex = c("M", "F", 
 "M"), Value = 1:3, City= c("Brookings",
 "Mumbai", "Paris"), Name = LETTERS[1:3])

df[,strsplit(col_names, ", ")[[1]]]

   Age Sex      City Name
1   5   M Brookings    A
2   3   F    Mumbai    B
3  25   M     Paris    C

Comments

0

The simple answer

If you have dataframe df and column name "Column_Name", you can access the column as a vector like this:

df[["Column_Name"]]

So, when you use the colnames(df) function, you can use the strings returned to access the columns you need. A very similar method works for multiple columns:

df[c("Column_1", "Column_2", "Column_3")]

Notice that using only one set of square brackets ([]) will return a sub-dataframe, while using two sets ([[]]) will return a vector.

In your situation, you may want to consider leaving out the paste() function. The following will work just fine:

col_names = c("Age", "Sex", "City", "Name")
col_data = df[col_names]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.