'I knew of one little DO loop that ran for 48 hours, cost $14,000 and did nothing' - Balfour and Marwick Related Commands: FOR - Loop commands FOR - Loop through a set of files in one folder FOR /R - Loop through files (recurse subfolders) FOR /L - Loop through a range of numbers FOR /F - Loop through. I'm working in a folder containing multiple sub-folders within R environment. I wanted a loop over the multiple sub-folders and then call an R script in each sub-folder for execution. I came up with the code below. But my code seems to be adding '.'
Today, I finally got inspired to deal with tons of datasets from the Tribunal Superior Eleitoral on the Brazilian elections. Midi files for yamaha clavinova. The cause of the delay for putting my finger on them was simply to avoid troubles with messy large text files. The set of data I collect consists of above 40GB of pure text files, which reports electoral results, candidates' profile, campaign revenues and expenditures etc. Therefore, if anything it may be a good example of using R for data management, and that it might be useful for students while dealing with messy datasets from everywhere.
The task can be stated as follows. Suppose you have a set of data files (data1.txt, data2.txt, […] ,data27.txt) which represents some data–or a subset data–sliced by states or electoral districts. What you want to do is simply stack every data file into a beautiful unique file for more aggregated analyses, or just releasing the computer from storing too many sliced data. In sum, the task is to obtain a table of all subsets; more complex cases will be addressed on later posts. This can be done by browsing to the directory where the files are, then looping through them importing and merging. Finally, the aggregated file can be written back to the disk.
The piece of code below does just that. The first line paste the path where R must look at for the files. The second line creates an empty data object to store each of the importing files if any. The third line reads the path to the files, and then a loop for reading each existing file of type '.txt' as table. The last line in the loop creates the final table by appending each subset that was imported into memory. Finally, the last part of the program, which is out of the loop for efficiency purpose, simply write the final table to the disk as a text file,delimiting the columns by semicolon ‘;'.
Today, I finally got inspired to deal with tons of datasets from the Tribunal Superior Eleitoral on the Brazilian elections. Midi files for yamaha clavinova. The cause of the delay for putting my finger on them was simply to avoid troubles with messy large text files. The set of data I collect consists of above 40GB of pure text files, which reports electoral results, candidates' profile, campaign revenues and expenditures etc. Therefore, if anything it may be a good example of using R for data management, and that it might be useful for students while dealing with messy datasets from everywhere.
The task can be stated as follows. Suppose you have a set of data files (data1.txt, data2.txt, […] ,data27.txt) which represents some data–or a subset data–sliced by states or electoral districts. What you want to do is simply stack every data file into a beautiful unique file for more aggregated analyses, or just releasing the computer from storing too many sliced data. In sum, the task is to obtain a table of all subsets; more complex cases will be addressed on later posts. This can be done by browsing to the directory where the files are, then looping through them importing and merging. Finally, the aggregated file can be written back to the disk.
The piece of code below does just that. The first line paste the path where R must look at for the files. The second line creates an empty data object to store each of the importing files if any. The third line reads the path to the files, and then a loop for reading each existing file of type '.txt' as table. The last line in the loop creates the final table by appending each subset that was imported into memory. Finally, the last part of the program, which is out of the loop for efficiency purpose, simply write the final table to the disk as a text file,delimiting the columns by semicolon ‘;'.
Sub Folder Or Subfolder
Could you please help me on this issue. I've already searched and I watched some videos, but it was not useful.I need help to loop through the files in a folder (200+ csv files). I am using missForest() to impute missing values. If I run the code for each single file, I have to do as following:
## main script for each single file
G1334108 <- read.csv(file.choose(), header = T)
G1334108.F <- missForest(G1334108, verbose = TRUE, maxiter = 5)
write.csv(G1334108.F$ximp, file = 'G1334108_F.csv')
I tried these below script codes to loop the function before writing here:
# 1st tryall.files <- list.files()
my.files <- grep('.*csv', all_files, value=T)
for(i in my.files){
# do your operations here
G1344108.Forest <- missForest(G1344108, verbose = TRUE, maxiter = 5)
# save
output.filename <- gsub('(.*?).csv', '1.csv', i)
write.table(G1344108.Forest$ximp, output.filename)
}## 2nd try
files <- list.files()lapply(files, function(x) {my.files <- read.csv('*.csv', header = T)missforest.out <- missForest(my.files, verbose = TRUE, maxiter = 5)write.csv(missforest.out$ximp, file = '*_F.csv')
}Thank you for the time.Best regards,Morteza
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.