Reputation: 231
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?
Upvotes: 1
Views: 219
Reputation: 886938
We can paste
them together to a single one with |
grep(paste(NAICS_pattern, collapse="|"), colnames( all_data ), value = TRUE )
Upvotes: 1