如何用dplyr重命名列?

hgqdbh6s  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(127)

我正在尝试使用dplyr的rename函数,但是我的列名是可变的,我想使用一个函数来设置列名,但是它不起作用。
我的代码:

library(dplyr)

data_hoje <- as.Date("2023-01-20")

teste %>%
  rename("Há 30 dias" = V1,
         "Há 7 dias" = V2,
         paste0(format(data_hoje, "%d/%b/%y")) = V3,
         " " = Delta)

我得到这个错误:
错误:中出现意外的"=":"" Há 7直径"= V2,粘贴0(格式(数据_地址," % d/% b/% y "))="
我的dput

structure(list(V1 = c("2022-12-20", "5.1908", "3.6700", "3.3729", 
"3.3400", NA, "5.2205"), V2 = c("2023-01-13", "5.3720", "3.8095", 
"3.4606", "3.3997", "3.2951", "5.3589"), V3 = c("2023-01-20", 
"5.4573", "3.8953", "3.5577", "3.5023", "3.5357", "5.4372"), 
    Delta = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)), class = "data.frame", row.names = c("Data", 
"2023", "2024", "2025", "2026", "2027", "Suavizada"))

我怎样才能使这个代码工作?

3bygqnnd

3bygqnnd1#

使用此命令:

teste %>% rename("Há 30 dias" := V1,
         "Há 7 dias" := V2,
         "{paste0(format(data_hoje, '%d/%b/%y'))}" := V3,
         " " := Delta)

参见vignette("programming")

相关问题