[英]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.