如何用向量值替换colnames-使用dplyr :: rename()函数 [英] How to replace colnames with vector values - using dplyr::rename() function

查看:151
本文介绍了如何用向量值替换colnames-使用dplyr :: rename()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试尽可能多地使用dplyr。现在,我有一个简单的向量,其中包含要在data.table的 colnames()中替换的字符串元素。

I'm trying to use dplyr as much as I can in my operations. Now I have a simple vector containing the string elements I would like to replace in the colnames() of my data.table.

我阅读了一些文章并尝试过,但无法理解它的外观。

I read some posts and tried, but can't get my head around how it should look like.

我的代码

> head(tickerData)
           AAK.ST.Open AAK.ST.High AAK.ST.Low AAK.ST.Close AAK.ST.Volume AAK.ST.Adjusted
2007-01-02       199.5       199.5      195.0        198.0         74400          159.54
2007-01-03       198.0       202.5      196.5        202.5         79100          163.17
2007-01-04       201.0       206.0      200.0        204.0        258500          164.38
2007-01-05       204.0       207.0      204.0        205.5         42100          165.59
2007-01-08       205.0       205.0      201.5        205.0        155300          165.18
2007-01-09       203.0       205.0      202.0        204.0        149000          164.38

> colName <- c('Open', 'High', 'Low', 'Close', 'Volume', 'Adj.Close')

> tickerData <- tickerData %>% ggplot2::fortify() %>%
+     # rename
+     rename(.dots = setNames(colName, 'new'))
Error: Arguments to rename must be unquoted variable names. Arguments .dots are not.

在这种情况下语法应如何显示?

How should the syntax look like in this case?

推荐答案

什么

library(dplyr)

tickerData %>%
  setNames(colName)

这篇关于如何用向量值替换colnames-使用dplyr :: rename()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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