使用数据框标题/名称重命名列名 [英] Renaming a column name, by using the data frame title/name

查看:355
本文介绍了使用数据框标题/名称重命名列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为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)




  cyl avg_mtcars_mpg 
< dbl> < DBL>
1 4 26.66364
2 6 19.74286
3 8 15.10000




  my_fnc(iris,Species,Petal.Width)




 种类avg_iris_Petal.Width 
1 setosa 0.246
2 versicolor 1.326
3 virginica 2.026



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")

    cyl avg_mtcars_mpg
  <dbl>          <dbl>
1     4       26.66364
2     6       19.74286
3     8       15.10000

my_fnc(iris, "Species", "Petal.Width")

     Species avg_iris_Petal.Width
1     setosa                0.246
2 versicolor                1.326
3  virginica                2.026

这篇关于使用数据框标题/名称重命名列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆