[R] Arithmetic on multiple files

Romain Francois romain.francois at dbmail.com
Wed Dec 2 18:09:19 CET 2009


About this:

foo <- function( files = c( "file1", "file2" ), nr = 3, nc = 3 ){
   out <- numeric( nr * nc )
   for( i in seq_along(files)){
     out <- out + scan( files[i] , what = numeric(0), quiet = TRUE )
   }
   matrix( out, nr = nr, nc = nc, byrow = T )
}

 > foo()
      [,1] [,2] [,3]
[1,]    5    5    5
[2,]    7    7    7
[3,]    9    9    9


This would have been more fun if you could store them all in memory at 
once (which I think you can't) :

 > files <- c("file1", "file2" )
 > matrices <- lapply( files, scan, what = numeric(0), quiet = TRUE )
 > matrix( Reduce( "+", matrices ), nr = 3, nc = 3, byrow = TRUE )
      [,1] [,2] [,3]
[1,]    5    5    5
[2,]    7    7    7
[3,]    9    9    9

Romain


On 12/02/2009 05:53 PM, Muhammad Rahiz wrote:
>
> Dear R-users,
>
> I'd like to perform arithmetic functions on 1000 files containing a
> 2000x2000 matrix. Can anyone advise?
>
> For example,
>
> File1 File2 Output
> 1 1 1 4 4 4 5 5 5
> 2 2 2 + 5 5 5 = 7 7 7
> 3 3 3 6 6 6 9 9 9
>
>
>
> Muhammad
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/Gq7i : ohloh
|- http://tr.im/FtUu : new package : highlight
`- http://tr.im/EAD5 : LondonR slides




More information about the R-help mailing list