[R] Difficulty importing data from PARI/GP

William Dunlap wdunlap at tibco.com
Fri Dec 21 22:02:00 CET 2012


By the way, you can leave out the write-to-file step, as read.table's text=
argument does the equivalent.  E.g.,

  > x <- "1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6\r\n"
  > p <- read.table(text=gsub(";","\n",x),sep=",")
  > str(p)
  'data.frame':   12 obs. of  6 variables:
   $ V1: int  1 1 1 1 1 1 1 1 1 1 ...
   $ V2: int  2 2 2 2 2 2 2 2 2 2 ...
   $ V3: int  3 3 3 3 3 3 3 3 3 3 ...
   $ V4: int  4 4 4 4 4 4 4 4 4 4 ...
   $ V5: int  5 5 5 5 5 5 5 5 5 5 ...
   $ V6: int  6 6 6 6 6 6 6 6 6 6 ...

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 jim holtman
> Sent: Friday, December 21, 2012 12:51 PM
> To: murfs
> Cc: r-help at r-project.org
> Subject: Re: [R] Difficulty importing data from PARI/GP
> 
> One way is to use 'readLines' to read in the file, change the ';' to
> '\n', write the file out and then read it back in:
> 
> > x <- readChar('/temp/test.txt', 1e6)
> > print(x)
> [1]
> "1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,
> 5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6\r\n"
> > x <- gsub(';', '\n', x)
> > writeChar(x, '/temp/testNew.txt')
> > # now read in the data
> > x.df <- read.table('/temp/testNew.txt', sep = ',')
> > x.df
>    V1 V2 V3 V4 V5 V6
> 1   1  2  3  4  5  6
> 2   1  2  3  4  5  6
> 3   1  2  3  4  5  6
> 4   1  2  3  4  5  6
> 5   1  2  3  4  5  6
> 6   1  2  3  4  5  6
> 7   1  2  3  4  5  6
> 8   1  2  3  4  5  6
> 9   1  2  3  4  5  6
> 10  1  2  3  4  5  6
> 11  1  2  3  4  5  6
> 12  1  2  3  4  5  6
> 
> 
> On Fri, Dec 21, 2012 at 12:39 PM, murfs <jm9461 at my.bristol.ac.uk> wrote:
> > I'm trying to import a matrix created in PARI/GP into R but am having
> > problems.
> > The data in the text file has entries separated by commas but the rows
> > themselves are separated by semicolons rathen than being on a new line. Is
> > there a way to get R to recognise that ";" means start a new row ?
> >
> >
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/Difficulty-importing-
> data-from-PARI-GP-tp4653736.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
> Data Munger Guru
> 
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
> 
> ______________________________________________
> 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