简体   繁体   English

在 R 中,如何在变量后命名 Dataframe 列?

[英]In R How Do I Name a Dataframe Column After a Variable?

As a part of a function I'm trying to create a dataframe and I want to name one of the columns after a variable.作为 function 的一部分,我正在尝试创建 dataframe 并且我想在变量之后命名其中一列。 The below is some dummy data and is the part that I'm stuck on.下面是一些虚拟数据,是我坚持的部分。

library(tidyverse)
graph <-  data.frame(brand = rep(c("A","B","C"), each = 3),
                    week_commencing = rep(as.Date(c('2020-03-01', '2020-04-01', '2020-05-01')), times = 3),
            sessions = sample(1000:2000,9),
            conv_rate = runif(9,0,1))

website = "A"
metric = "sessions"

graph %>% 
  filter(brand == (!!website)) %>%
  group_by(brand) %>% 
  summarise(metric = max(get(metric)),
            week_commencing = min(week_commencing),
            lab = "This Year") 

In the summarise function call I want the column name metric to be called sessions, I've tried using get(metric) and (.!metric) as part of the naming of the column but it doesn't work.在汇总 function 调用中,我希望将列名度量称为会话,我尝试使用 get(metric) 和 (.!metric) 作为列命名的一部分,但它不起作用。

Is this even possible to do in R?这甚至可以在 R 中做到吗? Any help would be appreciated.任何帮助,将不胜感激。

Do you mean something like this?你的意思是这样的吗?

library(tidyverse)
graph <-  data.frame(brand = rep(c("A","B","C"), each = 3),
                     week_commencing = rep(as.Date(c('2020-03-01', '2020-04-01', '2020-05-01')), times = 3),
                     sessions = sample(1000:2000,9),
                     conv_rate = runif(9,0,1))

website = "A"

metric = "sessions"

graph %>% 
  filter(brand == (!!website)) %>%
  group_by(brand) %>% 
  summarise(metric = max(get(metric)),
            week_commencing = min(week_commencing),
            lab = "This Year") %>% rename(sessions=metric)

# A tibble: 1 x 4
  brand sessions week_commencing lab      
  <fct>    <int> <date>          <chr>    
1 A         1819 2020-03-01      This Year

If you wish to use a variable as a name on the left hand side of a dplyr function, (without having to separately rename the column), you can use :!variable := instead of variable = , so in your case it would be:如果您希望使用变量作为dplyr function 左侧的名称(无需单独重命名列),您可以使用:!variable :=而不是variable = ,因此在您的情况下:

graph %>% 
  filter(brand == (!!website)) %>%
  group_by(brand) %>% 
  summarise(!!metric := max(get(metric)),
            week_commencing = min(week_commencing),
            lab = "This Year") 
#> # A tibble: 1 x 4
#>   brand sessions week_commencing lab      
#>  <chr>    <int> <date>          <chr>    
#> 1 A         1901 2020-03-01      This Year

We can also do this with我们也可以这样做

library(dplyr)   
graph %>%
   filter(brand == website) %>%
   group_by(brand) %>% 
   summarise(metric = max(!! rlang::sym(metric)),
             week_commencing = min(week_commencing),
             lab = "This Year") %>% rename_at(vars(metric), ~ 'sessions')
# A tibble: 1 x 4
  brand sessions week_commencing lab      
  <chr>    <int> <date>          <chr>    
1 A         1555 2020-03-01      This Year

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在R中:如何从字符串加上列名称再加上分类变量来创建数据框名称? - In R: How do I create a dataframe name from a string plus a column name plus categorical variable? 如何删除 R dataframe 中标题为 NA 的列/变量? - How do I remove a column/variable titled NA in R dataframe? 在 R 中,如何检查一个 dataframe 中的列名是否存在于另一个 dataframe 中,然后在该列中插入一个值? - In R, how do I check to see if a column name in one dataframe is present in another dataframe and then insert a value into that column? 如何将此堆叠变量的 dataframe 转换为 dataframe,在 R 中每列一个变量? (长到宽) - How do I transform this dataframe of stacked variables to a dataframe with one variable per column in R? (Long to Wide) 我可以使用变量来识别 R dataframe 中的列名吗? - Can I use a variable to identify a column name in an R dataframe? 如何使用向量重命名R数据框中的列以指示OLD列名? - How do I rename a column in a R dataframe using a vector to indicate the OLD column name? 在 R 中,如何在给定字符串名称的列表中修改 dataframe 列 - In R, how do I modify a dataframe column in a list given a string name 如何将多个数据框中的单个列重命名为 dataframe 的名称,它们位于 R 中? - How do I rename a single column in multiple dataframes to the name of the dataframe in which they reside in R? 如何使用 dataframe 中另一列的数据命名 R 中的文件下载? - How do I name file downloads in R using data from another column in dataframe? 使用变量名R在数据框中重命名列 - rename column in dataframe using variable name R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM