如何使用字符对象将r在r中重命名列?
我的数据框架就是这样:
#generic dataset
datatest <- data.frame(col1 = c(1,2,3,4), col2 = c('A', 'B', 'C', 'D'))
#character objects
name1 <- 'A'
name2 <- 'B'
我想使用name1和name2对象重命名列。这些动态更改代码,因此我无法使用以下内容:
#I DON'T WANT THIS
datatest %>% rename(A = col1, B = col2)
我想使用以下内容:
datatest %>% rename(name1 = col1, name2 = col2)
但是,数据表列最终分别成为'name1'和'name2',当它们应为a和B时。这是目前的数据表。
name1(我希望这是a) | name2(我希望这是b) |
---|---|
1 | A |
2 | B |
3 | C |
4 | D |
任何帮助都非常感谢。我也在Kable桌子上也有同样的问题。 提前致谢!
My data frame is as such:
#generic dataset
datatest <- data.frame(col1 = c(1,2,3,4), col2 = c('A', 'B', 'C', 'D'))
#character objects
name1 <- 'A'
name2 <- 'B'
I want to rename my columns using the name1 and name2 objects. These dynamically change in the code so I can't use the following:
#I DON'T WANT THIS
datatest %>% rename(A = col1, B = col2)
I want to use this:
datatest %>% rename(name1 = col1, name2 = col2)
but then the data table columns end up becoming 'name1' and 'name2' respectively, when they should be A and B. Here is the data table at the moment.
name1 (I want this to be A) | name2 (I want this to be B) |
---|---|
1 | A |
2 | B |
3 | C |
4 | D |
Any help is hugely appreciated. I have the same issue with kable tables too.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几个选项 -
rename_with
-Couple of options -
rename_with
-您可以尝试
You may try
这是使用
!setNames
的另外一个选项Here is one more option using
!!!setNames