Re: [R] how to skip last lines while reading the data in R

From: Romain Francois <rfrancois_at_mango-solutions.com>
Date: Mon, 28 Jan 2008 15:57:58 +0000

Hi,

What about this :

data <- read.table( pipe('head -n-2 data.txt') )

However, the aesthetics will only appeal to people having head (maybe it needs to be a recent version as well ...).

Cheers,

Romain

Barry Rowlingson wrote:
> Henrique Dallazuanna wrote:
>
>> Perhaps:
>>
>> data <- read.table(textConnection(rev(rev(readLines('data.txt'))[-(1:2)])))
>>
>>
>
> Euurgh! Am I the only one whose sense of aesthetics is enraged by
> this? To get rid of the last two items you reverse the vector, remove
> the first two items, then reverse the vector again?
>
> One liners are fine for R Golf games, but in the real world, I'd get
> the length of the vector and cut directly off the end. Consider these:
>
> # reverse/trim/reverse:
> rev1 <- function(x,n=100,m=5){
> for(i in 1:n){
> y=rev(rev(x)[-(1:m)])
> }
> return(y)
> }
>
> # get length, trim
> rev2 <- function(x,n=100,m=5){
> for(i in 1:n){
> y=x[1:(length(x)-m)]
> }
> return(y)
> }
>
> > system.time(rev1(1:1000,10000,5))
> [1] 1.864 0.008 2.044 0.000 0.000
> > system.time(rev2(1:1000,10000,5))
> [1] 0.384 0.008 0.421 0.000 0.000
>
>
> Result: faster, more directly readable code.
>
> ______________________________________________
> R-help_at_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.
>
>

-- 
Mango Solutions
data analysis that delivers

Tel:  +44(0) 1249 467 467
Fax:  +44(0) 1249 467 468
Mob:  +44(0) 7813 526 123

______________________________________________
R-help_at_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.
Received on Mon 28 Jan 2008 - 16:01:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jan 2008 - 16:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive