I have a list of data frames listofdfs
. To rename the columns of one of the dataframes singledf
in the list, the following code works:
colnames(listofdfs[["singledf"]]) <- paste(colnames(listofdfs[["singledf"]]), "singledf")
Aim
To rename all columns for all data frames in a list of dataframes, listofdfs
, to include the name of the dataframe in all of the respective column names.
Attempt 1
for (i in listofdfs){
colnames(listofdfs[i]) <- paste(colnames(listofdfs[i]), i)
}
This error occurs
Error in `*tmp*`[i] : invalid subscript type 'list'
Attempt 2
for (i in listofdfs){
newnames <- paste(colnames(listofdfs[i]), i)
colnames(bsl) <- newnames
}
This error occurs
No error is printed, however when I check one of the dataframes' columns the column names remain unchanged.
Attempt 3
for (i in listofdfs){
colnames(listofdfs[[i]]) <- paste(colnames(listofdfs[[i]]), i)
}
This error occurs
Error in listofdfs[[i]] : invalid subscript type 'list'
Below is a code that renames the column names of each data.frame in a list in a way so that names of data.frames are added to original column names.
# example data
a <- data.frame(col1 = 1:10, col2 = 10:1)
b <- data.frame(col_01 = 11:20, col_02 = 20:11)
# list of data.frames
list_of_df <- list(a, b)
# names of data.frames
names(list_of_df) <- c("a", "b")
# my sequence and names of data.frames in a list
my_seq <- seq_along(list_of_df)
my_list_names <- names(list_of_df)
# procedure
for (i in my_seq) {
names(list_of_df[[my_seq[i]]]) <-
paste(my_list_names[i], names(list_of_df[[my_seq[i]]]), sep = "_")
}
list_of_df
$a
a_col1 a_col2
1 1 10
2 2 9
3 3 8
4 4 7
5 5 6
6 6 5
7 7 4
8 8 3
9 9 2
10 10 1
$b
b_col_01 b_col_02
1 11 20
2 12 19
3 13 18
4 14 17
5 15 16
6 16 15
7 17 14
8 18 13
9 19 12
10 20 11
you are almost there. do something like this
for (i in seq_along(listofdfs)){
colnames(listofdfs[[i]]) <- paste(colnames(listofdfs[[i]]), i)
}
this should execute your logic of creating column names without any error.
Why
for (i in listofdfs){
colnames(listofdfs[[i]]) <- paste(colnames(listofdfs[[i]]), i)
}
Because you are expecting i
to be a index but rather it is a data.frame
itself. Debug using print
for (i in listofdfs){
print(class(i))
}
this is what you get
[1] "data.frame"
[1] "data.frame"
you cant subscript using a data.frame
. forloop with in
operator in R iterates along the individual elements and not their indexes. Hence we have to use seq_along
Hope this helps.