Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic input file parsing in R

Tags:

file

parsing

r

perl

I'm used to perl and new to R. I know you can read whole tables using read.table() but I wonder how can I use R to parse a single line from an input file.

Specifically, what is the equivalent to the following perl snippet:

open my $fh, $filename or die 'can't open file $filename';
my $line = <$fh>;
my ($first, $second, $third) = split ("\t", $line);
like image 392
David B Avatar asked Oct 14 '22 00:10

David B


2 Answers

Similar to the above would be:

filename <- 'your/file/name/here'
fh <- file( filename, open='rt' )
line <- readLines(fh, n=1 )
tmp <- strsplit(line, "\\t")
first <- tmp[[1]][1]; second <- tmp[[1]][2]; third <- tmp[[1]][3]

The file function creates a connection to the file and opens it, the opening is optional, but if you don't open the file then when you read from it it will open then close the file again, if you open the file then it remains open and the next read continues from where the previous left on (closest match to what Perl would be doing above).

The readLines function will read the specified number of lines (1 in this case) then strsplit works basically the same as the Perl split function.

R does not have the multiple assign like Perl (it is often best to just keep the results together anyways rather than splitting into multiple global variables).

like image 163
Greg Snow Avatar answered Oct 20 '22 15:10

Greg Snow


In general, you should use scan to do this, or in more complex cases read the whole file with readLines and parse it manually with strsplits, greps and stuff.

In your case:

scan(filename,character(0),nmax=3)->d
first<-d[1];d[2]->second;third<-d[3]
like image 43
mbq Avatar answered Oct 20 '22 17:10

mbq