[R] problem in reading trajectory file

Phil Spector spector at stat.berkeley.edu
Mon Mar 15 19:59:48 CET 2010


Arnaud -
    A reproducible example would be nice, but something 
like this might work, assuming your data is in a file
called 'traj.txt':

> dat = readLines('traj.txt')
> triallines = grep('trial',dat)
> starts = triallines + 2
> ends = c(triallines[-1] - 1,length(dat))
> thetrials = sub('# *trial *(\\d+)','\\1',dat[triallines],perl=TRUE)
> pieces = mapply(function(start,end,trial)
                   data.frame(trial,read.table(textConnection(dat[start:end]))),
                   starts,ends,thetrials,SIMPLIFY=FALSE)
> answer = do.call(rbind,pieces)
> names(answer) = c('trial','t','X','Y')

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Mon, 15 Mar 2010, arnaud chozo wrote:

> Hi,
>
> I'm trying to read some trajectory files (text files) which have the form:
>
> # trial n
> # t      X      Y
>  0      1       2
> 0.2     1       3
> 0.4    1.2     4
> ...
>
> # trial n+1
> # t      X      Y
>  0      1       2
> 0.2     1.3     3.3
> 0.4     1.5     5
> ...
>
> ...
>
> where the symbol # means that the line is not a numeric value, but I still
> need to read the number of the trial (n=1, 2, etc.).
> I wonder if there is an easy way to read this file in order to plot the
> trajectory for each trial.
>
> Thanks,
> Arnaud
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list