[R] Importing data from text file with mixed format

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 25 23:14:46 CET 2009


This solution uses strapply in gsubfn. It assumes the timepoints are
1, 2, 3, ... (although later we remove this restriction just in case).

The first line reads in myfile. The second line reads the numeric rows
into matrix s.  The third line reads in the column names.  The fourth
line converts to data frame and adds the Timepoint column numbering
the first data set 1, the second 2, etc.  (also known as long form)
and the fifth line is the result.


library(gsubfn)
L <- readLines("myfile")
s <- strapply(L, "^([0-9]+) +([0-9.]+) +([0-9.]+) *$", c, simplify = rbind)
colnames(s) <- c(read.table("myfile", FALSE, nrow = 1, skip = 1, as.is = TRUE))
DF <- transform(s, Timepoint = cumsum(DF$ObjectNumber == 1))
split(DF[-4], DF[4])


If the timepoints are not necessarily 1, 2, 3, ... then replace the
last line with this (which extracts the timepoints and assigns them):

Timepoint <- c(strapply(L, "Timepoint *([0-9]+)", as.numeric, simplify = rbind))
DF$Timepoint <- Timepoint[DF$Timepoint]
split(DF[-4], DF[4])


On Sat, Oct 24, 2009 at 11:31 PM, delnatan <delnatan at gmail.com> wrote:
>
> Hi,
> I'm having difficulty importing my textfile that looks something like this:
>
> #begin text file
> Timepoint 1
> ObjectNumber     Volume     SurfaceArea
> 1                      5.3          9.7
> 2                      4.9          8.3
> 3                      5.0          9.1
> 4                      3.5          7.8
>
> Timepoint 2
> ObjectNumber     Volume     SurfaceArea
> 1                      5.1          9.0
> 2                      4.7          8.9
> 3                      4.3          8.3
> 4                      4.2          7.9
>
> ... #goes on to Timepoint 80
>
> How would I import this data into a list containing data.frame for each
> timepoint?
> I'd like my data to be organized like this:
>
>>myList
> [[1]]
>   ObjectNumber     Volume     SurfaceArea
> 1  1                      5.3          9.7
> 2  2                      4.9          8.3
> 3  3                      5.0          9.1
> 4  4                      3.5          7.8
>
> [[2]]
>  ObjectNumber     Volume     SurfaceArea
> 1 1                      5.1          9.0
> 2 2                      4.7          8.9
> 3 3                      4.3          8.3
> 4 4                      4.2          7.9
>
> -Daniel
> --
> View this message in context: http://www.nabble.com/Importing-data-from-text-file-with-mixed-format-tp26045031p26045031.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