报错类型
Error: All arguments must be named
plyr中的rename和dplyr中的rename用法是不同的.
rename(data, c(old=new))
rename(data, new = old)
比如, 默认的是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语言 列名重命名内容请搜索html5模板网以前的文章希望大家以后多多支持html5模板网!