Reputation: 529
More help needed please for what seems to me straightforward but I just can't find an elegant solution for.
Say I have a dataframe with 15 columns (coming from a cbind of 3 csv's) where the colnames are the same for each source CSV....
> filenames <- list("file1.csv","file2.csv","file3.csv")
> df <- do.call("cbind", lapply(filenames, read.csv, header = TRUE))
> colnames(df)
[1] "Col1" "Col2" "Col3" "Col4" "Col5" "Col1" "Col2" "Col3" "Col4" "Col5"
[11] "Col1" "Col2" "Col3" "Col4" "Col5"
What I need to be able to do is prefix each column name with the source filename so that the whole thing is workable.
I can do this manually but ideally (as the number of sources and therefore columns can change) it should be really simple to work out which filename goes with which columns. It seems to me that following does the job BUT I'm sure there is a simpler route probably by looping over filePrefix and colnames(df).
> filePrefix <- lapply(seq_along(filenames), function(i) gsub(".csv","",filenames[i]))
> newColNames <- lapply(seq_along(colnames(df)[1:5]), function(i) paste(filePrefix[1],"_",colnames(df)[i],sep=""))
> newColNames <- c(newColNames, lapply(seq_along(colnames(df)[6:10]), function(i) paste(filePrefix[2],"_",colnames(df)[i],sep=""))
> newColNames <- c(newColNames, lapply(seq_along(colnames(df)[11:15]), function(i) paste(filePrefix[3],"_",colnames(df)[i],sep=""))
> colnames(df) <- newColNames
> colnames(df)
[1] "file1_Col1" "file1_Col2" "file1_Col3" "file1_Col4" "file1_Col5" "file2_Col1" "file2_Col2" "file2_Col3" "file2_Col4" "file2_Col5"
[11] "file3_Col1" "file3_Col2" "file3_Col3" "file3_Col4" "file3_Col5"
Can anyone help?
Upvotes: 0
Views: 238
Reputation: 21425
Maybe you can use this,assuming all your dataframes have 5 columns:
colnames(df)<-paste(rep(gsub(".csv","",filenames),each=5),colnames(df),sep="_")
it just gets the filenames using your gsub, repeats them five times and then concatenates them with the original column names
Upvotes: 1