Kootseeahknee
Kootseeahknee

Reputation: 49

R: How can I apply column name changes to many data frames at once?

I am trying to replace certain words in the column headers of over 100 data frames.I have a string that works perfectly:

names(singledataframe) <- gsub(x = names(singledataframe), pattern = "Int'l", 
replacement = "International") 

I am trying to apply this to all the frames I've made. I used c()to refer to them under AllFrames. I was trying to make a function of the string that works on one frame and apply it to AllFrames, with a for loop.

ChangeName1 <- function(singledataframe) {
  gsub(x = names(singledataframe), pattern = "Int'l", replacement = "International") 
  }

then something like:

for (i in 1:length(names(AllFrames))) {lapply(i, ChangeName1)
}

I get no errors, but randomly checking individual frames shows the intended effect did not take.I've tried so many variations, but I'm stuck! I'm thinking maybe I need to edit the c() that refers to AllFrames in a better way.

Upvotes: 1

Views: 318

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50668

You're not assigning and returning the changed data.frame from your function. Try:

ChangeName1 <- function(singledataframe) {
    names(singledataframe) <- gsub(
        x = names(singledataframe), 
        pattern = "Int'l", 
        replacement = "International");
    return(singledataframe);
}

Update

Let's assume your data.frames are stored in a list; I will use some randomly generated data:

set.seed(2017);
lst <- list(
    data.frame(rnorm(10), rnorm(10)),
    data.frame(rnorm(10), rnorm(10)));
lst <- lapply(lst, function(x) { colnames(x) <- c("A", "Int'l"); x });
#[[1]]
#              A      Int'l
#1   1.434201478  0.3427681
#2  -0.077291959  1.5724254
#3   0.739137231 -0.7467347
#4  -1.758604727  0.3066498
#5  -0.069825227 -1.4304858
#6   0.451905527  1.1944265
#7  -1.958366456 -0.4820681
#8  -0.001524259  1.3178624
#9  -0.265336001 -1.1298316
#10  1.563222619 -0.9263514
#
#[[2]]
#             A      Int'l
#1   0.14070941 -0.6831744
#2  -0.85223255  0.2410894
#3   1.90339718  0.5894319
#4  -1.64818594 -0.9306249
#5   0.75962477  0.2974619
#6   0.06229087  3.1372771
#7  -0.38903119 -1.0275347
#8   0.63666563  1.9418028
#9  -0.86778544 -0.2779562
#10  0.09873361 -0.2215888

Then we can simply lapply the function changeName1 to all list elements:

lapply(lst, ChangeName1);
#[[1]]
#              A International
#1   1.434201478     0.3427681
#2  -0.077291959     1.5724254
#3   0.739137231    -0.7467347
#4  -1.758604727     0.3066498
#5  -0.069825227    -1.4304858
#6   0.451905527     1.1944265
#7  -1.958366456    -0.4820681
#8  -0.001524259     1.3178624
#9  -0.265336001    -1.1298316
#10  1.563222619    -0.9263514
#
#[[2]]
#             A International
#1   0.14070941    -0.6831744
#2  -0.85223255     0.2410894
#3   1.90339718     0.5894319
#4  -1.64818594    -0.9306249
#5   0.75962477     0.2974619
#6   0.06229087     3.1372771
#7  -0.38903119    -1.0275347
#8   0.63666563     1.9418028
#9  -0.86778544    -0.2779562
#10  0.09873361    -0.2215888

Upvotes: 3

Related Questions