1

I have a dataframe all_data with 14 columns that need to be combined into 4 columns.

So far, I have made objects for the raw columns name strings.

name_pattern <- c( "Geographic.area.name", "Geographic Area Name")
VoS_pattern <- c( "Total.value.of.shipment", "value of shipments")
NAICS_pattern <- c( "NAICS.code", "NAICS code")
industry_pattern <- c("Meaning.of.", "Meaning of NAICS code")

Here, for example, I have 5 columns that are contained by the strings in VoS_pattern, that I need to combine into one column.

I need to create objects containing all individual columns which will be united to one column. When there is only one string assigned the object, such as NAICS_pattern <- "NAICS.code" as opposed to NAICS_pattern <- c( "NAICS.code", "NAICS code"), the following works

NAICS_col_names <- grep( NAICS_pattern, colnames( all_data ), value = TRUE )

Unfortunately, it does not work when there are multiple strings assigned to the object, and the warning I receive is:

In grep(NAICS_pattern, colnames(all_data), value = TRUE) : argument 'pattern' has length > 1 and only the first element will be used

Any solutions for this?

0

1 Answer 1

1

We can paste them together to a single one with |

grep(paste(NAICS_pattern, collapse="|"), colnames( all_data ), value = TRUE )
Sign up to request clarification or add additional context in comments.

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.