我有一组列名,如下所示
"fb_metrics.3.name"
"fb_metrics.3.period"
"fb_metrics.3.values.0.value.share"
"fb_metrics.3.values.0.value.like"
"fb_metrics.3.values.0.value.comment"
"fb_metrics.3.title"
"fb_metrics.3.description"
名称和期间每个只有一个唯一值。例如
> df[,"fb_metrics.3.name"]
[1] post_storytellers_by_action_type
Levels: post_storytellers_by_action_type
> df[, "fb_metrics.3.period"]
[1] lifetime
Levels: lifetime
我想像这样重命名列3,4,5
[1] "post_storytellers_by_action_type.lifetime.share"
[2] "post_storytellers_by_action_type.lifetime.like"
[3] "post_storytellers_by_action_type.lifetime.comment"
我已经像这样管理了替换位
i=3
new_column_name <- paste(as.character(df[1,paste("fb_metrics.",
i,".name", sep = "")]),as.character(df[1,paste("fb_metrics.",
i,".period", sep = "")]), sep = "." )
sub(pattern = ".*value",replacement = new_column_name,x = colnames(df[,
grep(paste("fb_metrics.",i,".values.*",sep = ""), column_names)]))
我已经像这样提取了要替换的列
column_names <- colnames(df)
list_of_columns <- colnames(df[,grep("fb_metrics.3.values.*", column_names)])
我的问题是我应该如何使用刚刚创建的列名重命名提取的列?还有,有更简单的方法吗?
编辑:
好的,我把它重命名为
library(data.table)
setnames(df, old = list_of_columns, new = sub(pattern = ".*value",replacement = new_column_name,x = colnames(df[,grep(paste("fb_metrics.",i,".values.*",sep = ""), column_names)])))
但有没有更简单的方法来完成整个过程?
答案 0 :(得分:1)
当面对复杂/多次替换时,您可以通过创建自定义函数来简化该过程 使用不同的输入参数并按顺序应用它以达到所需的列名结构。
<强> InputData:强>
DF1 = data.frame(fb_metrics.3.name="post_storytellers_by_action_type",fb_metrics.3.period="lifetime",fb_metrics.3.values.0.value.share=1:5,fb_metrics.3.values.0.value.like=1:5,
fb_metrics.3.values.0.value.comment=1:5,stringsAsFactors=FALSE)
DF1
# fb_metrics.3.name fb_metrics.3.period fb_metrics.3.values.0.value.share
#1 post_storytellers_by_action_type lifetime 1
#2 post_storytellers_by_action_type lifetime 2
#3 post_storytellers_by_action_type lifetime 3
#4 post_storytellers_by_action_type lifetime 4
#5 post_storytellers_by_action_type lifetime 5
# fb_metrics.3.values.0.value.like fb_metrics.3.values.0.value.comment
#1 1 1
#2 2 2
#3 3 3
#4 4 4
#5 5 5
<强> CustomFunction:强>
#Custom function to aid replacements
fn_modify_str = function(pattern="a",replacement="str",suffix=".",inputString="abcd") {
gsub(pattern,paste0(replacement,suffix),inputString)
}
<强> Pararmeters:强>
inputColumn1 = "^fb_metrics.3.name$"
inputColumn2 = "^fb_metrics.3.period$"
replacePattern1 = "fb_metrics.3.values.0[.]"
replacePattern2 = "value"
uniqValue1 = unique(DF1[,grep(inputColumn1,colnames(DF1))])
uniqValue1
#[1] "post_storytellers_by_action_type"
uniqValue2 = unique(DF1[,grep(inputColumn2,colnames(DF1))])
uniqValue2
#[1] "lifetime"
<强>替换强>
#apply replacements using custom function sequentially for both patterns
strPart1 = fn_modify_str(pattern=replacePattern1,replacement = uniqValue1,suffix=".",inputString = colnames(DF3))
strPart2 = fn_modify_str(pattern=replacePattern2,replacement = uniqValue2,suffix="",inputString = strPart1)
#you can rename columns in the same dataset by just simple assignment
#colnames(DF1) = strPart2
#OR, you can create a backup dataset and rename the columns in the new DF
DF2 = DF1
colnames(DF2) = strPart2
<强>输出:强>
DF2
# fb_metrics.3.name fb_metrics.3.period post_storytellers_by_action_type.lifetime.share
#1 post_storytellers_by_action_type lifetime 1
#2 post_storytellers_by_action_type lifetime 2
#3 post_storytellers_by_action_type lifetime 3
#4 post_storytellers_by_action_type lifetime 4
#5 post_storytellers_by_action_type lifetime 5
# post_storytellers_by_action_type.lifetime.like post_storytellers_by_action_type.lifetime.comment
#1 1 1
#2 2 2
#3 3 3
#4 4 4
#5 5 5