Renaming the column name of data frame in R language

Error type
Error: All arguments must be named

The use of rename in plyr is different from that in dplyr.

plyr::rename

rename(data, c(old=new))

dplyr::rename

rename(data, new = old)

Example

For example, the default is plyr’s rename. Run the following command, and an error will be reported :

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"))

result

> 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)

result:

> 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

Either

or with plyr modified in the first way :

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"))

result:

> 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

done!!!!!!

key points, dplyr is the new name in front, the old name is placed behind, and without quotes, not c(), more convenient!!

In addition, the select in dplyr can also select + the name, directly specifying the number of columns!!

d <- data.frame(old1=1:3, old2=4:6, old3=7:9)
d
select(d,one=1,three=3)

result:

> 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

Read More: