报错类型
Error: All arguments must be named
plyr中的rename和dplyr中的rename用法是不同的.
plyr::rename
rename(data, c(old=new))
dplyr::rename
rename(data, new = old)
Example
比如, 默认的是plyr的rename, 运行下面命令, 会报错:
d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
d
library(tidyverse)
rename(d, c("old2"="two", "old3"="three"))
rename(d, c(old2="two", old3="three"))
结果
> d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
> d
old1 old2 old3
1 1 4 7
2 2 5 8
3 3 6 9
> library(tidyverse)
> rename(d, c("old2"="two", "old3"="three"))
Error: All arguments must be named
> rename(d, c(old2="two", old3="three"))
Error: All arguments must be named
正确的打开方式:
d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
d
rename(d, two=old2, three=old3)
结果:
> d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
> d
old1 old2 old3
1 1 4 7
2 2 5 8
3 3 6 9
> rename(d, two=old2, three=old3)
old1 two three
1 1 4 7
2 2 5 8
3 3 6 9
或者使用plyr按第一种方式修改:
d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
d
library(tidyverse)
plyr::rename(d, c("old2"="two", "old3"="three"))
plyr::rename(d, c(old2="two", old3="three"))
结果:
> d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
> d
old1 old2 old3
1 1 4 7
2 2 5 8
3 3 6 9
> library(tidyverse)
> plyr::rename(d, c("old2"="two", "old3"="three"))
old1 two three
1 1 4 7
2 2 5 8
3 3 6 9
> plyr::rename(d, c(old2="two", old3="three"))
old1 two three
1 1 4 7
2 2 5 8
3 3 6 9
搞定!!!
关键点, dplyr是新名称放在前面, 旧名称放在后面, 而且不用引号, 不用c(), 更方便!!!
另外, dplyr中的select还可以选择+ 该名称, 直接指定列数!!!
d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
d
select(d,one=1,three=3)
结果:
> d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
> d
old1 old2 old3
1 1 4 7
2 2 5 8
3 3 6 9
> select(d,one=1,three=3)
one three
1 1 7
2 2 8
3 3 9
到此这篇关于R语言中对数据框的列名重命名的实现的文章就介绍到这了,更多相关R语言 列名重命名内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!