I have multiple data.frames that all have different names but same column names, for example:
filenames <- c(1:10)
for (i in filenames){
filenames[i] <- paste("df",filenames[i],sep="")
assign((filenames[i]), data.frame(matrix(rnorm(20), nrow=10)))
}
How can one append the column names within each df with the name of that df?
The output would be:
> head(df1)
df1_X1 df1_X2
1 0.2343486 0.2191546
2 0.5042413 2.0720167
3 -0.1082240 0.7376801
4 0.2346395 0.4677974
5 1.1559909 -1.1432890
6 -1.5554426 -0.2309197
> head(df2)
df2_X1 df2_X2
1 -2.79824632 -0.1879618
2 1.93410571 0.3012066
3 0.72948663 0.2139871
4 0.59290004 1.1093813
5 0.04826737 -0.4062374
6 -0.78271090 -1.2870127
etc...
Here is my initial solution, which does not work:
for (i in length(filenames)){
colnames(filenames[i]) <- paste(filenames[i], colnames(filenames[i]), sep = "_")
}