使用另一列的值重命名列
Renaming columns with a value from another column
我有一组列名如下
"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)])))
但是有没有更简单的方法来完成整个过程?
当面临 complex/multiple 替换时,您可以通过创建自定义函数来简化流程
使用不同的输入参数并按顺序应用它以达到所需的列名称结构。
输入数据:
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
自定义函数:
#Custom function to aid replacements
fn_modify_str = function(pattern="a",replacement="str",suffix=".",inputString="abcd") {
gsub(pattern,paste0(replacement,suffix),inputString)
}
参数:
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
我有一组列名如下
"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)])))
但是有没有更简单的方法来完成整个过程?
当面临 complex/multiple 替换时,您可以通过创建自定义函数来简化流程 使用不同的输入参数并按顺序应用它以达到所需的列名称结构。
输入数据:
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
自定义函数:
#Custom function to aid replacements
fn_modify_str = function(pattern="a",replacement="str",suffix=".",inputString="abcd") {
gsub(pattern,paste0(replacement,suffix),inputString)
}
参数:
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