Re: [R] How to read several text files at once!

From: Petr Klasterecky <klaster_at_karlin.mff.cuni.cz>
Date: Wed, 09 May 2007 11:27:41 +0200

A for-loop looks like the best solution here. An outline:

csum <- 0 #or a matrix of 0
for (i in (1:253)){

tmpa <- read.table(file=paste("A",i,sep=""), header=TRUE)
tmpb <- read.table(file=paste("B",i,sep=""), header=TRUE)
tmpc <- tmpa + tmpb #or whatever operation you like
write.table(tmpc, file=paste("C",i,sep="")) csum <- csum + ...
}#end for
write.table(csum, file="csum.dat")

See ?write.table for more details on writing data to files. Petr

Faramarzi Monireh napsal(a):
> Dear R users,
> I am a beginner in R. I have 506 text files (data frame) in one folder namely DATA. The files are called A1 to A253 (253 files) and B1 to B253 (another 253 files). Each file has two columns; V1 (row number)
> and V2 (the value for each row name). Now I would like to add the values of
> V2 in each A-file with its relative value in B-file and save it as a
> new data frame named as C (e.g. C1 with V1 (row number) and V2
> (A1$V2+B1$V2) ). Therefore, at the end I will have 253 C files
> (C1 to C253). I also would like to sum a number of the C files with each other (e.g. C1+ C2+ +C50) and save as a new file like C_sum.
>
> I already tried to write a short script to do all together but it did not
> work. I only was able to do for each C file separately. The main problem
> is that I do not know how to read several text files and how to use those
> files to make C files and afterwards C-sum files. I would be gratful if somebody can help me to write a short script to do all together.
> Thank you very much in advance for your cooperation,
> Monireh
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch mailing list
> 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.
>
>

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic

______________________________________________
R-help_at_stat.math.ethz.ch mailing list
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.
Received on Wed 09 May 2007 - 09:33:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 May 2007 - 10:31:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.