[R] How do I read multiple rows of different lengths?
Phil Spector
spector at stat.berkeley.edu
Thu Oct 28 00:55:04 CEST 2010
Here's how I'd approach the problem.
First, read the file:
> a = readLines('testOut')
Each different type of line can be identified by the
string at the beginning of the line, based on these
patterns. It seems that you only want the numbers,
so we'll convert them as we extract them:
> pats = c('^[0-9]','^bp','^origScore','^fit','^residuals','^parameters')
> getnums = function(pat){
+ txt = grep(pat,a,value=TRUE)
+ lapply(lapply(strsplit(txt,'\t'),function(x)x[2:length(x)]),as.numeric)
+ }
> result = lapply(pats,getnums)
> names(result) = c('first','x','y','fit','res','parameters')
So to reproduce your x, y, fit, and res:
x = result$x[[1]]
y = result$orig[[1]]
fit = result$fit[[1]]
res = result$res[[1]]
Of course, you can access any of the other results by changing the subscript.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Wed, 27 Oct 2010, smcguffee wrote:
>
> Using the text itself works, although slightly annoying. I ended up just
> processing the text each time I wanted a value inside it, which turns out to
> be about the same thing. The key to processing the text ended up being a
> command: library("CGIwithR") followed by another command scanText(textLine)
> to process each text line. Below is an example where if I change n, I can
> look at any section of data. It bewilders me as to why R doesn't come with
> scanText without loading some sort of library automatically loaded, but hey,
> this eventually worked for me and it only took about a full day to figure it
> out. I don't know of any other software that could do the same task without
> the same effort in figuring out how to do it. Plus, R is free and whatnot,
> so I think it is turning out to be worth the headache of easy things not
> being obvious.
> fitLines=readLines("testOut")
> n=1
> x=as.numeric(scanText(fitLines[(n-1)*6+2])[2:length(scanText(fitLines[(n-1)*6+2]))])
> y=as.numeric(scanText(fitLines[(n-1)*6+3])[2:length(scanText(fitLines[(n-1)*6+3]))])
> fit=as.numeric(scanText(fitLines[(n-1)*6+4])[2:length(scanText(fitLines[(n-1)*6+4]))])
> res=as.numeric(scanText(fitLines[(n-1)*6+5])[2:length(scanText(fitLines[(n-1)*6+5]))])
> plot(x,y)
> lines(x,fit)
> legend(x[1],max(y),legend=c(scanText(fitLines[(n-1)*6+6])))
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-do-I-read-multiple-rows-of-different-lengths-tp3015986p3016219.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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