Gabriela Ramirez
Gabriela Ramirez

Reputation: 13

Add different suffix to column names on multiple data frames in R

I'm trying to add different suffixes to my data frames so that I can distinguish them after I've merge them. I have my data frames in a list and created a vector for the suffixes but so far I have not been successful.

data2016 is the list containing my 7 data frames

new_names <- c("june2016", "july2016", "aug2016", "sep2016", "oct2016", "nov2016", "dec2016")
data2016v2 <- lapply(data2016, paste(colnames(data2016)), new_names)

Upvotes: 1

Views: 903

Answers (2)

Brian Stamper
Brian Stamper

Reputation: 2263

You could use an index number within lapply to reference both the list and your vector of suffixes. Because there are a couple steps, I'll wrap the process in a function(). (Called an anonymous function because we aren't assigning a name to it.)

data2016v2 <- lapply(1:7, function(i) {
  this_data <- data2016[[i]] # Double brackets for a list
  names(this_data) <- paste0(names(this_data), new_names[i]) # Single bracket for vector
  this_data # The renamed data frame to be placed into data2016v2
})

Notice in the paste0() line we are recycling the term in new_names[i], so for example if new_names[i] is "june2016" and your first data.frame has columns "A", "B", and "C" then it would give you this:

> paste0(c("A", "B", "C"), "june2016")
[1] "Ajune2016" "Bjune2016" "Cjune2016"

(You may want to add an underscore in there?)

As an aside, it sounds like you might be better served by adding the "june2016" as a column in your data (like say a variable named month with "june2016" as the value in each row) and combining your data using something like bind_rows() from the dplyr package, running it "long" instead of "wide".

Upvotes: 0

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

Your query is not quite clear. Therefore two solutions. The beginning is the same for either solution. Suppose you have these four dataframes:

df1x <- data.frame(v1 = rnorm(50),
                   v2 = runif(50))
df2x <- data.frame(v3 = rnorm(60),
                   v4 = runif(60))
df3x <- data.frame(v1 = rnorm(50),
                   v2 = runif(50))
df4x <- data.frame(v3 = rnorm(60),
                   v4 = runif(60))

Suppose further you assemble them in a list, something akin to your data2016using mgetand ls and describing a pattern to match them:

my_list <-  mget(ls(pattern = "^df\\d+x$"))

The names of the dataframes in this list are the following:

names(my_list)
[1] "df1x" "df2x" "df3x" "df4x"

Solution 1:

Suppose you want to change the names of the dataframes thus:

new_names <- c("june2016", "july2016","aug2016", "sep2016")

Then you can simply assign new_namesto names(my_list):

names(my_list) <- new_names

And the result is:

names(my_list)
[1] "june2016" "july2016" "aug2016"  "sep2016"

Solution 2:

You want to add the new_names literally as suffixes to the 'old' names, in which case you would use pasteor paste0 thus:

names(my_list) <- paste0(names(my_list), "_", new_names)

And the result is:

names(my_list)
[1] "df1x_june2016" "df2x_july2016" "df3x_aug2016"  "df4x_sep2016" 

Upvotes: 1

Related Questions