Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rename_if() together with starts_with() to prefix certain columns [duplicate]

My data looks like this:

#>           Artist          Album Year
#> 1        Beatles  Sgt. Pepper's 1967
#> 2 Rolling Stones Sticky Fingers 1971

And my question should be quite simple. I'm trying to use rename_if to prefix only the columns that start with the letter "A". So my desired output is:

#>       df1_Artist      df1_Album Year
#> 1        Beatles  Sgt. Pepper's 1967
#> 2 Rolling Stones Sticky Fingers 1971

You can see that "Year" should not be prefixed.

This is my attempt, but it's not quite working. Am I using starts_with incorrectly? Should I try break it into two lines so I can understand it more clearly? The purrr style functions I'm still learning, so it's not always intuitive to me yet.

df1 %>% rename_if(starts_with("A"), .funs = ~ paste0(df1, .))
#> Error in df1 %>% rename_if(starts_with("A"), .funs = ~paste0(df1, .)): could not find function "%>%"

Code for data input:

df1 <- data.frame(stringsAsFactors=FALSE,
      Artist = c("Beatles", "Rolling Stones"),
       Album = c("Sgt. Pepper's", "Sticky Fingers"),
        Year = c(1967, 1971)
)
like image 570
Jeremy K. Avatar asked Jun 21 '19 02:06

Jeremy K.


People also ask

How do I change multiple column names 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 change column names in R with dplyr?

rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to change the column from old to a new name. The following example renames the column from id to c1 . The operator – %>% is used to load the renamed column names to the data frame.

How do I change column headers in R?

How to change column headers of a data-frame in R? colnames () function can be used to change the column names of a data-frame column in R. colnames () function can be used for changing one column name at a time, also all the column names can be changed at once.

How do I rename a column in a variable in R?

colnames() is the method available in R base which is used to rename columns/variables present in the data frame. By using this you can rename a column by index and name. Alternatively, you can also use name() method.


1 Answers

rename_if expects a logical vector as predicate function. starts_with selects variables based on their name. Use base startsWith instead which returns a logical vector based on prefix

library(dplyr)
df1 %>% rename_if(startsWith(names(.), "A"), ~paste0("df1_", .))

#      df1_Artist      df1_Album Year
#1        Beatles  Sgt. Pepper's 1967
#2 Rolling Stones Sticky Fingers 1971

Or if you want to stay in tidyverse you can also use str_detect

df1 %>% rename_if(stringr::str_detect(names(.), "^A"), ~paste0("df1_", .))

To use starts_with we can use rename_at which has vars argument.

df1 %>% rename_at(vars(starts_with("A")), ~paste0("df1_", .))
like image 172
Ronak Shah Avatar answered Nov 14 '22 15:11

Ronak Shah