Using Lapply to Change Column Names of a List of Data Frames

Using lapply to change column names of a list of data frames

You can also use setNames if you want to replace all columns

df1 <- data.frame(A = 1:10, B= 11:20)
df2 <- data.frame(A = 21:30, B = 31:40)

listDF <- list(df1, df2)
new_col_name <- c("C", "D")

lapply(listDF, setNames, nm = new_col_name)
## [[1]]
## C D
## 1 1 11
## 2 2 12
## 3 3 13
## 4 4 14
## 5 5 15
## 6 6 16
## 7 7 17
## 8 8 18
## 9 9 19
## 10 10 20

## [[2]]
## C D
## 1 21 31
## 2 22 32
## 3 23 33
## 4 24 34
## 5 25 35
## 6 26 36
## 7 27 37
## 8 28 38
## 9 29 39
## 10 30 40

If you need to replace only a subset of column names, then you can use the solution of @Jogo

lapply(listDF, function(df) {
names(df)[-1] <- new_col_name[-ncol(df)]
df
})

A last point, in R there is a difference between a:b - 1 and a:(b - 1)

1:10 - 1
## [1] 0 1 2 3 4 5 6 7 8 9

1:(10 - 1)
## [1] 1 2 3 4 5 6 7 8 9

EDIT

If you want to change the column names of the data.frame in global environment from a list, you can use list2env but I'm not sure it is the best way to achieve want you want. You also need to modify your list and use named list, the name should be the same as name of the data.frame you need to replace.

listDF <- list(df1 = df1, df2 = df2)

new_col_name <- c("C", "D")

listDF <- lapply(listDF, function(df) {
names(df)[-1] <- new_col_name[-ncol(df)]
df
})

list2env(listDF, envir = .GlobalEnv)
str(df1)
## 'data.frame': 10 obs. of 2 variables:
## $ A: int 1 2 3 4 5 6 7 8 9 10
## $ C: int 11 12 13 14 15 16 17 18 19 20

Using lapply to set column names for a list of data frames?

It seems you want to update the original dataframes. In that case, your list MUST be named. ie check the code below.

List <- list(a = a, b = b, c = c, d = d)
list2env(lapply(List, setNames, nm = headers), globalenv())

Now if you call a you will note that it has been updated.

Using lapply to change column names of list of dataframes with different column names

You can create a dataframe with the information of from and to and with lapply use setNames to match and replace the column names :

lookup_names <- data.frame(from = c("A", "B", "D", "E", "G", "H"), 
to = c("this", "that", "he", "she", "him", "her"))

lookup_names
# from to
#1 A this
#2 B that
#3 D he
#4 E she
#5 G him
#6 H her

lapply(my_list, function(x)
setNames(x, lookup_names$to[match(names(x), lookup_names$from)]))

#[[1]]
# this that
#1 1 10
#2 2 11
#3 3 12
#4 4 13
#...
#...

#[[2]]
# he she
#1 20 30
#2 21 31
#3 22 32
#4 23 33
#5 24 34
#....

Altering the column names of dataframes in a list with lapply

Using base R, you can use Map.

Map(function(x, y) setNames(x, paste(names(x), y, sep = ".")), list1, names(list1))

Here is another way using purrr. The i functions (e.g., imap, imodify) will pass the name of the list item in as parameter .y along with the list contents in .x.

library(purrr)

imodify(list1, ~ set_names(.x, paste(names(.x), .y, sep = ".")))

How to change column names of many dataframes in R?

Create a named list, apply the function and use list2env to reflect those changes in the original dataframes.

library(nycflights13)

files <- dplyr::lst(mtcars, flights, airports)
result <- lapply(files, ChangeNames)
list2env(result, .GlobalEnv)

list - rename specific data.frames column with lapply

You need to use {} and return x:

lst <- lapply(lst, function(x) {colnames(x)[7] <- 'new_name'; x}) 

Or

lst <- lapply(lst, function(x) {
colnames(x)[7] <- 'new_name'
x
})

As a reproducible example, you could use

lapply(list(iris, iris), function(x) {colnames(x)[3] <- "test"; x})

Converting column names to upper case in a list of data frames using lapply explanation

Since you are modifying the object x (or in this case only the colnames of x) inside the function definition, you have to return the modified object x. This is happening by using ;x which can be read as a new line only returning the object x

Using `lapply` to rename selected columns in list

In this case, I would suggest to use mapply instead

mapply(function(x, y) {names(x)[2] <- y; x}, templist, allobj)

#[[1]]
# fruit df1 price
#1 apple Japan 32
#2 Orange China 53
#3 Pear Nigeria 12

#[[2]]
# grocery df2 name favourite.food invoice
#1 Durian Korea Mark Apple XD1
#2 Apple Japan John ORANGE XD2
#3 Watermelon Malaysia Tammy Cakes XD3

#[[3]]
# address df3
#1 address1 USA
#2 address2 UK
#3 address3 China

If you want to use lapply you could use x as the index to subset both templist as well as allobj since for 1st list we want name from 1st value of allobj, for 2nd list we want 2nd value of allobj and so on.

lapply(seq_along(templist), function(x) {
names(templist[[x]])[2] <- allobj[x]
templist[[x]]
})


Related Topics



Leave a reply



Submit