Re: [R] Read last line of a file

2011-05-04 Thread Petr Savicky
On Wed, May 04, 2011 at 02:17:29AM -0700, Joel wrote:
> Hi dear R users.
> 
> I got a file that I need to extract the third column (or word) of the last
> line of files that has a diffrent amounts of rows.
> It works with 
> 
> x<-read.tables("file")
> x[1,3] 
> 
> This returns the proper result but as the files is large this takes time and
> uses memory that is just unneccery.
> 
> p<-read.table(textConnection(system("tail -1 file",intern=TRUE)))
> p[1,3]
> 
> This also returns the proper result but then requires the system to be unix
> based witch is quite silly if you ask me. Would rather just use R commands.
> 
> So Im wondering if anyone of you got a better way of reading the last line
> of a file and extracting the third column (or word) of that line.

Hi.

The following reads the file into memory, but it is more
efficient than read.table(), since it does no parsing of
the file as a whole.

  x <- readLines("file")
  strsplit(x[length(x)], " +")[[1]][3]

Hope this helps.

Petr Savicky.

__
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.


[R] Read last line of a file

2011-05-04 Thread Joel
Hi dear R users.

I got a file that I need to extract the third column (or word) of the last
line of files that has a diffrent amounts of rows.
It works with 

x<-read.tables("file")
x[1,3] 

This returns the proper result but as the files is large this takes time and
uses memory that is just unneccery.

p<-read.table(textConnection(system("tail -1 file",intern=TRUE)))
p[1,3]

This also returns the proper result but then requires the system to be unix
based witch is quite silly if you ask me. Would rather just use R commands.

So Im wondering if anyone of you got a better way of reading the last line
of a file and extracting the third column (or word) of that line.

Best regards
//Joel Damberg



--
View this message in context: 
http://r.789695.n4.nabble.com/Read-last-line-of-a-file-tp3494963p3494963.html
Sent from the R help mailing list archive at Nabble.com.

__
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.