如何重命名数据框中的所有列,以包含列表中所有数据框的数据名称? [英] How to rename all columns in a dataframe to include the name of the data for all dataframes in a list?
问题描述
我有一个数据框列表 listofdfs
。要重命名列表中的某个数据框 singledf
的列,可以使用以下代码:
colnames(listofdfs [[singledf]])< - paste(colnames(listofdfs [[singledf]]),singledf)
/ pre>
目标
要重命名所有数据框的所有列数据框列表
listofdfs
,以在所有相应列名称中包含数据框的名称。
尝试1
我在listofdfs){
$
colnames(listofdfs [i])< - paste(colnames(listofdfs [i]),i)
}
$ b发生此错误
错误in`* tmp *`[i]:无效的下标类型'list'
尝试2
for(i in listofdfs){
newnames< - paste(colnames(listofdfs [i ]),i)
colnames(bsl)< - 新名称
}
发生此错误
没有打印错误,但是当我检查其中一个数据框的列时,列名保持不变。 >
尝试3
for(i in listofdfs ){
pre>
colnames(listofdfs [[i]])< - paste(colnames(listofdfs [[i]]),i)
}
发生此错误
listofdfs [[i]]中的错误:无效的下标类型'list'
$ p $下面是一个代码,以一种方式重命名列表中每个data.frame的列名,以便data.frames的名字添加到原始列名称中。
#示例数据
a < - data.frame(col1 = 1:10 ,col2 = 10:1)
b< - data.frame(col_01 = 11:20,col_02 = 20:11)
#data.frames
list_of_df< ; - 列表(a,b)
#data.frames
名称(list_of_df)< -c(a,b)
#我的序列和data.frames在列表中的名称
my_seq< - seq_along(list_of_df)
my_list_names< - names(list_of_df)
#procedure
(for 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
I have a list of data frames
listofdfs
. To rename the columns of one of the dataframessingledf
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
这篇关于如何重命名数据框中的所有列,以包含列表中所有数据框的数据名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!