0

This feels like it should be pretty straightforward! Apologies if I am missing the obvious answer - I haven't found it when searching.

I have merged two sets of data - let's say one relates to new mothers and one to babies. In their original dataframes, they didn't specify in the columns whether they were the mothers or the babies, but now they are combined, for some of the columns it is worth specifying to avoid confusion. I don't necessarily want to do this for ALL columns, because in some instances it is obvious and would make the column name too long and clunky, and in others they relate to both.

So for e.g. I have something like this, except with a lot more columns:

family <- c("Ali", "Baker", "Cruz") sex <- c("FEMALE", "MALE", "FEMALE") first_name <- c("Aylin", "Betty", "Camila") age <- c(30, 27, 36) area <- c("Aberdeen", "Birmingham", "Cardiff") births_df <- data.frame(family, sex, first_name, age, area) 

In this example, the first and last columns relates to both parties, the second to the baby, and the rest to the mother.

I want to rename columns 3 and 4 so they say "mother_" before the existing column name.

I can obviously do this individually, as below, but this is not really viable if I have to repeat this for lots of columns.

births_df <- rename(births_df, c("first_name" = "mother_first_name", "age" = "mother_age" )) 

I have tried to write a function to do this:

rename_cols_mother <- function(data_f, column_name) { plyr::rename(data_f, c(column_name = paste("staff_", column_name))) } 

However, I am pretty new to functions and I am clearly doing something wrong as I get the following error when I try to then use the function:

births_df <- rename_cols_mother(births_df, c("first_name", "age")) 

The following from values were not present in x: column_name1, column_name2

And of course there might be a way to do this without needing a function!

1
  • 1
    do you need names(births_df)[3:4] <- paste0("mother_", names(births_df)[3:4]) ? Commented Oct 16, 2018 at 9:46

2 Answers 2

2

with dplyr :

library(dplyr) births_df %>% rename_at(3:4,~paste0("mother_", .)) # family sex mother_first_name mother_age area # 1 Ali FEMALE Aylin 30 Aberdeen # 2 Baker MALE Betty 27 Birmingham # 3 Cruz FEMALE Camila 36 Cardiff 

Also work :

births_df %>% rename_at(c("first_name", "age"),~paste0("mother_", .)) births_df %>% rename_at(vars(first_name, age),~paste0("mother_", .)) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I used your first option and this worked. I wasn't aware of rename_at before but that is what I needed.
0

Another approach using rename_at(), vars() and matches()

library(dplyr) births_df <- births_df %>% rename_at(vars(matches("first_name|age")), ~ str_replace(., "^", "mother_")) # family sex mother_first_name mother_age area # 1 Ali FEMALE Aylin 30 Aberdeen # 2 Baker MALE Betty 27 Birmingham # 3 Cruz FEMALE Camila 36 Cardiff 

We find any columns matching first_name or age, then using str_replace() in the beginning of string to add mother_. This approach works in this case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.