[R] Importing data from text file with mixed format
jim holtman
jholtman at gmail.com
Sun Oct 25 18:19:09 CET 2009
try this:
> # read in the file
> x <- readLines(textConnection("#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"))
> # delete blank lines
> blanks <- grep("^\\s*$", x)
> if (length(blanks) > 0) x <- x[-blanks]
> # determine where "Timepoint" occurs in the text vector
> tp <- grep("^Timepoint", x)
> # append length+1 to the result
> tp <- c(tp, length(x) + 3)
> input <- textConnection(x)
> # skip to the first "Timepoint" if not first line
> if (tp[1] != 1) readLines(input, n=tp[1] - 1)
[1] "#begin text file"
> result <- list()
> # repeat for each Timepoint
> for (numLines in diff(tp)){ # repeat for number of lines to read
+ id <- readLines(input, n=1)
+ result[[id]] <- read.table(input, header=TRUE, nrows=numLines - 2)
+ }
> result
$`Timepoint 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
$`Timepoint 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
> closeAllConnections()
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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list