[R] R more Unix-like?

Robert Citek rwcitek at alum.calberkeley.org
Thu May 11 08:39:20 CEST 2006


On May 10, 2006, at 10:40 PM, Gabor Grothendieck wrote:

> Try this on Windows or the corresponding ls command on UNIX:
>
>   readLines(pipe("cmd /c dir"))

I tried this from within R:

   readLines(pipe("bash -c ls"))

and this from the command line:

   $ echo 'readLines(pipe("bash -c ls"))' | R -q --no-save

both of which produced a listing of the current folder.  So one  
strategy would be to build up the command pipe within the R program:

   $ echo 'for (i in readLines(pipe("seq 1 10")))
           print(sprintf("foo %s", i))' | R

While not exactly the solution I was looking for, it did allow me to  
do perform some simple summary stats on one field in a large data set:

$ wc -l ../data/foo.txt
13628161 ../data/foo.txt

$ echo 'summary(as.numeric(readLines(pipe("< ../data/foo.txt cut -f  
10 | tail +2")))); proc.time()' | R

R > summary(as.numeric(readLines(pipe("< ../data/foo.txt cut -f 10 |  
tail +2")))); proc.time()
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   114.5   862.8  1316.0  2030.0  2111.0 65540.0
[1] 59.323  0.964 60.283  8.324  8.324

All that in about a minute.  Very nice.

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software.  Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent




More information about the R-help mailing list