我想为多个数据框的每一列添加一个后缀,该后缀将作为数据框本身的名称
df1 = data.frame(a=c(1,2,3),b=c(4,5,6))
df2 = data.frame(X=c(1,2,3),Y=c(4,5,6))
到目前为止,我可以单独进行:
colnames(df1) <- paste(colnames(df1), deparse(substitute(df1)), sep = "_")
colnames(df2) <- paste(colnames(df2), deparse(substitute(df2)), sep = "_")
> df1
a_df1 b_df1
1 1 4
2 2 5
3 3 6
> df2
X_df2 Y_df2
1 1 4
2 2 5
3 3 6
但这在此循环中不起作用:
for (df in list(df1,df2)) {
colnames(df) <- paste(colnames(df), deparse(substitute(df)), sep = "_")
}