[R] How to read in this data format?
Liaw, Andy
andy_liaw at merck.com
Thu Mar 1 17:27:33 CET 2007
You can't expect general-purpose tools like read.table in R to be able
to deal with highly specialized file format. Here's what I'd start. It
doesn't put data in the format you specified exactly, but I doubt you'll
need that. This might be sufficient for your purpose:
dat <- readLines(file("yourdata.dat"))
## Get rid of blank lines.
dat <- dat[dat != ""]
scan.lines <- grep("Scan", dat)
## Chop off the header rows.
dat <- dat[scan.lines[1]:length(dat)]
scan.lines <- scan.lines - scan.lines[1] + 1
lines.per.scan <- c(scan.lines[-1], length(dat) + 1) - scan.lines
## Split the data into a list, with each scan taking up one component.
dat <- split(dat, rep(seq(along=lines.per.scan), each=lines.per.scan))
## Process the data one scan at a time.
result <- lapply(dat, function(x) {
x <- strsplit(x, "\t")
rtime <- x[[2]][2] # second field of second line
t(matrix(as.numeric(do.call(rbind, c(rtime, x[-(1:2)]))), ncol=2))
})
This is what I get from the data you've shown:
R> result
$`1`
[,1] [,2] [,3] [,4]
[1,] 0.017 399.8112 399.8742 399.9372
[2,] 0.017 184.0000 0.0000 152.0000
$`2`
[,1] [,2] [,3] [,4]
[1,] 0.021 399.8112 399.8742 399.9372
[2,] 0.021 181.0000 1.0000 153.0000
Note that you probably should avoid using numbers as column names in a
data frame, even if it's possible.
Andy
From: Bart Joosen
>
> Hi,
>
> I recieved an ascii file, containing following information:
>
> $$ Experiment Number:
> $$ Associated Data:
>
> FUNCTION 1
>
> Scan 1
> Retention Time 0.017
>
> 399.8112 184
> 399.8742 0
> 399.9372 152
> ....
>
> Scan 2
> Retention Time 0.021
>
> 399.8112 181
> 399.8742 1
> 399.9372 153
> .....
>
>
> I would like to import this data in R into a dataframe, where
> there is a column time, the first numbers as column names,
> and the second numbers as data in the dataframe:
>
> Time 399.8112 399.8742 399.9372
> 0.017 184 0 152
> 0.021 181 1 153
>
> I did take a look at the read.table, read.delim, scan, ...
> But I 've no idea about how to solve this problem.
>
> Anyone?
>
>
> Thanks
>
> Bart
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>
>
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments,...{{dropped}}
More information about the R-help
mailing list