The pipeline metaphor enabled by packages like dplyr and magrittr is incredibly useful and does great things for making your code readable in R (a daunting task!)
How can one make a pipeline that ended with renaming all the variables in a data frame to a pre-determined list?
Here is what I tried. First, simple sample data to test on:
> library(dplyr)
> iris %>% head(n=3) %>% select(-Species) %>% t %>% as.data.frame -> test.data
> test.data
1 2 3
Sepal.Length 5.1 4.9 4.7
Sepal.Width 3.5 3.0 3.2
Petal.Length 1.4 1.4 1.3
Petal.Width 0.2 0.2 0.2
This doesn't work:
> test.data %>% rename(a=1,b=2,c=3)
Error: Arguments to rename must be unquoted variable names. Arguments a, b, c are not.
I wasn't able to figure out the precise meaning of this error from reading the documentation on rename
. My other attempt avoids an error by using curly braces to define a code block, but the renaming doesn't actually happen:
> test.data %>% { names(.) <- c('a','b','c')}
rename(a='1', b='2', c='3')