Reputation: 1120
I am curious if it's possible to do such loop. I'm not so familiar with looping so please let me know if I can do it and how to do it.
I have few csv files stored in a:
> tbl
[1] "F1.csv" "F10.csv" "F11.csv" "F12.csv"
[5] "F13.csv" "F14.csv" "F15.csv" "F16.csv"
[9] "F17.csv" "F18.csv" "F19.csv" "F2.csv"
[13] "F20.csv" "F3.csv" "F4.csv" "F5.csv"
[17] "F6.csv" "F7.csv" "F8.csv" "F9.csv"
That's how one of those files look like:
http://imageshack.com/a/img132/1524/w5a5.jpg
All of them are very similar. Look at the columns which I marked with red "1".
Than I created an empty matrix where I would like to keep all of the important data from all of those csv files:
## Create an empty matrix for EOD
name_cols <- paste0("F", rep(1:20, each = 4), "_", 1:4)
name_columns <- c("Description", name_cols)
mat_master_EOD <- matrix(0, nrow = length(data_rd[,1]),ncol = 81)
colnames(mat_master_EOD) <- name_columns
rownames(mat_master_EOD) <- data_rd[,1]
## Create an empty matrix for EON
name_cols <- paste0("F", rep(1:20, each = 4), "_", 1:4)
name_columns <- c("Description", name_cols)
mat_master_EON <- matrix(0, nrow = length(data_rd[,1]),ncol = 81)
colnames(mat_master_EON) <- name_columns
rownames(mat_master_EON) <- data_rd[,1]
That's how it looks like:
http://imageshack.com/a/img854/5193/u85m.jpg
Now, we can come back to the first picture and marked cells. I'd like to put the data from the F1.csv file by the "Accession".
That's what I am expecting to get:
http://imageshack.com/a/img41/1015/dcm6.jpg
So, what happened ? The data from the columns J-M from the F1.csv files were moved to the new created matrix called mat_master_EOD to columns C-F (named F1_1, F1_2, etc).
As you see I put a lot of effort to write that post. Please let me know how it should be done. What's my goal ?
To keep all of the important data for me in one csv file.
Upvotes: 0
Views: 78
Reputation: 5955
There seems to be several questions you want to answer. I will answer the one in the title - looping over files.
There is convenient function 'list.files'
# list all files in d:/temp starting with 'test'
list.files('d:/temp/', pattern='^test.')
[1] "test1.dbf" "test1.shp" "test1.shx" "testfile.csv" "testfile.dat" "testfile.txt"
In your case it would be:
filelist <- list.files(path, pattern='^F\d+\.csv')
Than you can loop over those filenames and do whatever you want (read.csv(filelist[1])
etc.)
for (filename in filelist){
tempdata <- read.csv(filename)
# perform some action with the data
}
Upvotes: 1