Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to Use Dplyr::Rename_At Together with "Starts_with" in a Function

Writer Matthew Barrera
library(tidyverse)

Using the sample data below, I have two dataframes - Df1 and Df2. I'm attempting to create a simple function that selects columns by string - in this case columns in both dataframes that start with "Person" as well as all columns that contain "Phone".

Next, I would like to rename the "Person" column in both dataframes to "Id".

However, I'm having trouble getting the rename function to work correctly.

Here is a first attempt...

Funs<-function(Df){
Df%>%select(starts_with("Person"),contains("Phone"))%>%
rename_at(vars(starts_with("Person"),"Id"))
}

Below are some other variations of "rename" that I tried, but didn't work...

rename_at(vars(starts_with("Person"),funs("Id"=.)))
rename("Id"=names(.)[1])

How should I correct this? I tried to find similar questions on this site since it seems simple enough, but I couldn't find anything that works...

Sample Data:

`Person #`<-c(199,148,148,145,177,165,144,121,188,188,188,111)
`Phone #1`<-c(6532881717,6572231223,6541132112,6457886543,6548887777,7372222222,6451123425,6783450101,7890986543,6785554444,8764443344,6453348736)
`Phone #2`<-c(NA,NA,NA,NA,NA,7372222222,NA,NA,NA,6785554444,NA,NA)
Animals<-c("Cat","Dog","Elephant","Giraffe","Bird","Snake","Skunk","Raccoon","Moose","Turtle","Beaver","Porcupine")
Df1<-data.frame(`Person #`,`Phone #1`,`Phone #2`, Animals)
`Person ID #`<-c(199,148,142,145,177,165,144,121,182,109,188,111)
`Phone s 1`<-c(6532881717,6572231223,6541132112,6457886543,6548887777,7372222222,6451123425,6783450101,7890986543,6785554400,8764443344,6453348736)
`Phone s 2`<-c(NA,NA,NA,NA,NA,7372222222,NA,NA,NA,6785554444,NA,NA)
Animals<-c("Cat","Dog","Elephant","Giraffe","Bird","Snake","Skunk","Raccoon","Moose","Turtle","Beaver","Porcupine")
Df2<-data.frame(`Person ID #`,`Phone s 1`,`Phone s 2`, Animals)
1

2 Answers

These two options work, though seems very fragile to rename in this way; It only works if you have exactly one column that meets the condition, i.e. starts_with('Person') :

Df1 %>% rename_at(vars(starts_with('Person')), ~ 'ID')
Df1 %>% rename_at(1, ~ 'ID')

Both give:

# ID Phone..1 Phone..2 Animals
#1 199 6532881717 NA Cat
#2 148 6572231223 NA Dog
#3 148 6541132112 NA Elephant
# ...

Or use funs:

Df1 %>% rename_at(vars(starts_with("Person")),funs(function(.) 'ID'))

The above did not work for me. This example does run:

tribble( ~col_name_1, ~col_name_2_foo, ~col_name_3_foo, "a", 1, 3, "b", 2, 2, "c", 3, 1
) %>%
rename_at( vars(ends_with("_foo")), function(x) { toupper(x) }
)
# A tibble: 3 x 3 col_name_1 COL_NAME_2_FOO COL_NAME_3_FOO <chr> <dbl> <dbl>
1 a 1 3
2 b 2 2
3 c 3 1

rename_at(vars(ends_with("_foo")), toupper) also works if the function you want to use is already defined.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy