On Apr 23, 2010, at 6:07 PM, Ryan Garner wrote:

> 
> How do I pass a filename as an argument to the system command to count the
> number of records in that file? I only know how to do it by hardcoding it.
> 
> HARDCODING EXAMPLE
>> test <- matrix(1:20,ncol=5)
>> write(x = test,file = "test.txt")
>> records <- as.numeric(system("cat test.txt | wc -l",intern = TRUE))
>> records
> [1] 4
> 
> ??? NON-HARCODING EXAMPLE ???
>> file = "test.txt"
>> records <- as.numeric(system("cat file | wc -l",intern = TRUE))
> cat: file: No such file or directory
>> records <- as.numeric(system("cat" test.txt "| wc -l",intern = TRUE))
> Error: unexpected symbol in "records <- as.numeric(system("cat" test.txt"
> 
> TFYH


See ?paste

file <- "test.txt"
cmd <- paste("cat", file, "| wc -l")

> cmd
[1] "cat test.txt | wc -l"

Then use:

as.numeric(system(cmd, intern = TRUE))

HTH,

Marc Schwartz

______________________________________________
R-help@r-project.org 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.

Reply via email to