简体   繁体   中英

How to combine columns in a dataframe based on strings contained in the column names?

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?

We can paste them together to a single one with |

grep(paste(NAICS_pattern, collapse="|"), colnames( all_data ), value = TRUE )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM