Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename multiple variables within a pipeline

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')}
like image 361
David M. Perlman Avatar asked Feb 26 '16 16:02

David M. Perlman


People also ask

How do I mass rename a column in R?

To change multiple column names by name and by index use rename() function of the dplyr package and to rename by just name use setnames() from data. table . From R base functionality, we have colnames() and names() functions that can be used to rename a data frame column by a single index or name.

How do I rename a column in R pipe?

To rename a column in R, you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B” again, you can run the following code: rename(dataframe, B = A) . That was it; we are getting ready to practice how to change the column names in R.

How do you rename multiple variables in SAS?

Rename Multiple Variables You can use the RENAME option also to change the name of multiple variables (at once). The RENAME option is a dataset option that you can use in the DATA statement and SET statement. You write the old variable names followed by an equal sign and the new variable names, all between parenthesis.


1 Answers

'1','2','3'You were correct except use setNames {stats} instead of rename (zx8754 answered in your comment before me)

setNames: This is a convenience function that sets the names on an object and returns the object. It is most useful at the end of a function definition where one is creating the object to be returned and would prefer not to store it under a name just so the names can be assigned.

Your example (Close just change rename with setNames)

iris %>% 
   head(n=3) %>% 
   select(-Species) %>% 
   t %>% 
   as.data.frame %>% 
   rename(a=1,b=2,c=3)

Answer

iris %>% 
   head(n=3) %>% 
   select(-Species) %>%
   t %>%
   as.data.frame %>%
   setNames(c('1','2','3'))

Another Example

name_list <- c('1','2','3')

iris %>% 
   head(n=3) %>% 
   select(-Species) %>%
   t %>%
   as.data.frame %>%
   setNames(name_list)
like image 69
mtelesha Avatar answered Sep 17 '22 22:09

mtelesha