简体   繁体   English

如何根据列名中包含的字符串组合 dataframe 中的列?

[英]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.我有一个 dataframe all_data有 14 列需要组合成 4 列。

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.例如,在这里,我有 5 列包含在VoS_pattern中的字符串中,我需要将它们合并为一列。

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当只有一个字符串分配了 object 时,例如NAICS_pattern <- "NAICS.code"而不是NAICS_pattern <- c( "NAICS.code", "NAICS code") ,以下工作

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:不幸的是,当有多个字符串分配给 object 时,它不起作用,并且我收到的警告是:

In grep(NAICS_pattern, colnames(all_data), value = TRUE): argument 'pattern' has length > 1 and only the first element will be used在 grep(NAICS_pattern, colnames(all_data), value = TRUE) 中:参数 'pattern' 的长度 > 1 并且仅使用第一个元素

Any solutions for this?有什么解决方案吗?

We can paste them together to a single one with |我们可以用|将它们paste到一个中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R - 按列过滤行 - 包含在其他数据框中的列名 - R - Filter rows by columns - column names contained in an other dataframe 如何根据 dataframe 中的列将列名称添加到空间多边形? - How to add columns names to a spatialpolygon based on column from a dataframe? 合并具有相似列名/相似列字符串的列 - Reprex - Combine Columns with Similar Column Names / Similar Column Strings - Reprex 根据 dataframe 的列名和 R 中的后续列删除列 - Remove columns of dataframe based on column names of itself and following columns in R 根据列名的向量在列表中按列逐列设置子数据集并汇总列 - subset dataframe by column in a list based on a vector of column names and summarize the columns R:根据数据框另一列中的不同信息合并列 - R: Combine columns based on different information in another column of a dataframe 如何根据 R 中的列名组合列? - How to combine columns based on column name in R? 根据列名称向数据框添加行,并将NA添加至空列 - Adding rows to a dataframe based on column names and add NA to empty columns R 根据列名中的日期值重新排列 dataframe 中的列 - R Rearrange columns in dataframe based on date values in column names 如何使用R根据向量中的部分列名称从数据框中提取所有列 - How to extract all columns from a dataframe based upon partial column names in a vector using R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM