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
fromvalues were not present inx: column_name1, column_name2
And of course there might be a way to do this without needing a function!
names(births_df)[3:4] <- paste0("mother_", names(births_df)[3:4])?