[R] How to read this data properly?

William Dunlap wdunlap at tibco.com
Sat Mar 3 19:06:06 CET 2012


It is a bit unclear from your posting exactly what your data
is.  I'm assuming that you have an R dataset, a single character
string called Data, made with a command like
Dat = "1 2 3 4 5
6 7 8 9 10
11 12 13 14 15"
(If you have a file, say "Dat.R", containing those three lines of text then
run source("Dat.R") and you be in the above situation.)

I'm also assuming that you want to intuit the number of columns
in the matrix from the layout of numbers in the string contained
in Dat.

Your string contained blank lines between every "real" line.
I suspect someone's mailer put them there, but you can get
rid of them with
  tDat <- gsub("\n+", "\n", Dat)
Next, break your string into "lines" and then each line into "words"
so you can count the number of entries in each line and make sure
there are the the same number of entries in each line.
  lines <- strsplit(tDat, "\n")[[1]]
  words <- strsplit(lines, "[[:space:]]+")
  nWordsPerLine <- unique(vapply(words, FUN=length, FUN.VALUE=0))
  stopifnot(length(nWordsPerLine) ==1) 
Finally, convert the "words" to numbers and make the matrix
  m <- matrix(as.numeric(unlist(words)), ncol=nWordsPerLine, byrow=TRUE)
  print(m)
       [,1] [,2] [,3] [,4] [,5]
  [1,]    1    2    3    4    5
  [2,]    6    7    8    9   10
  [3,]   11   12   13   14   15
 
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bogaso
> Christofer
> Sent: Saturday, March 03, 2012 7:15 AM
> To: r-help at r-project.org
> Subject: [R] How to read this data properly?
> 
> Dear all, I have been given a data something like below:
> 
> 
> 
> Dat = "2 3 28.3 3.05 8 3 3 22.5 1.55 0 1 1 26.0 2.30 9 3 3 24.8 2.10 0
> 
> 3 3 26.0 2.60 4 2 3 23.8 2.10 0 3 2 24.7 1.90 0 2 1 23.7 1.95 0
> 
> 3 3 25.6 2.15 0 3 3 24.3 2.15 0 2 3 25.8 2.65 0 2 3 28.2 3.05 11
> 
> 4 2 21.0 1.85 0 2 1 26.0 2.30 14 1 1 27.1 2.95 8 2 3 25.2 2.00 1
> 
> 2 3 29.0 3.00 1 4 3 24.7 2.20 0 2 3 27.4 2.70 5 2 2 23.2 1.95 4"
> 
> 
> 
> 
> 
> I want to create a matrix out of those data for my further calculations. I
> have tried with readLines() but got error:
> 
> 
> 
> > readLines(Dat)
> 
> Error in file(con, "r") : cannot open the connection
> 
> In addition: Warning message:
> 
> In file(con, "r") :
> 
>   cannot open file '2 3 28.3 3.05 8 3 3 22.5 1.55 0 1 1 26.0 2.30 9 3 3 24.8
> 2.10 0
> 
> 3 3 26.0 2.60 4 2 3 23.8 2.10 0 3 2 24.7 1.90 0 2 1 23.7 1.95 0
> 
> 3 3 25.6 2.15 0 3 3 24.3 2.15 0 2 3 25.8 2.65 0 2 3 28.2 3.05 11
> 
> 4 2 21.0 1.85 0 2 1 26.0 2.30 14 1 1 27.1 2.95 8 2 3 25.2 2.00 1
> 
> 2 3 29.0 3.00 1 4 3 24.7 2.20 0 2 3 27.4 2.70 5 2 2 23.2 1.95 4': No such
> file or directory
> 
> 
> 
> 
> 
> Can somebody help to put that data in some workable format?
> 
> 
> 
> Thanks and regards,
> 
> 
> 	[[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