问题描述
我有一个名为Something的数据框。我正在使用总结对其中一个数字列进行聚合,我希望该列的名称包含Something - 列名称中的数据框标题。示例:
temp< - %>%
group_by(Month)%>%
summary(avg_score = mean(score))
但是我想将聚合列命名为avg_Something_score。这是否有意义?
动态生成新列名称似乎更有意义,必须在 setNames
中硬编码数据框的名称。也许像下面的函数,它需要一个数据框,一个分组变量和一个数字变量:
library(dplyr)
库(lazyeval)
my_fnc = function(data,group,value){
df.name = deparse(substitute(data))
data%>%
group_by_(group)%>%
summarise_(avg = interp(〜mean(v),v = as.name(value)))%> %
重命名_(。dots = setNames(avg,paste0(avg_,df.name,_,value)))
}
现在让我们在两个不同的数据框上运行该功能:
my_fnc(mtcars,cyl,mpg)
my_fnc(iris,Species,Petal.Width)
I have a data frame called "Something". I am doing an aggregation on one of the numeric columns using summarise, and I want the name of that column to contain "Something" - data frame title in the column name.
Example:
temp <- Something %>%
group_by(Month) %>%
summarise(avg_score=mean(score))
But i would like to name the aggregate column as "avg_Something_score". Did that make sense?
It seems like it makes more sense to generate the new column name dynamically so that you don't have to hard-code the name of the data frame inside setNames
. Maybe something like the function below, which takes a data frame, a grouping variable, and a numeric variable:
library(dplyr)
library(lazyeval)
my_fnc = function(data, group, value) {
df.name = deparse(substitute(data))
data %>%
group_by_(group) %>%
summarise_(avg = interp(~mean(v), v=as.name(value))) %>%
rename_(.dots = setNames("avg", paste0("avg_", df.name, "_", value)))
}
Now let's run the function on two different data frames:
my_fnc(mtcars, "cyl", "mpg")
my_fnc(iris, "Species", "Petal.Width")
这篇关于使用数据框标题/名称重命名列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!