Skip to content

Difficulty importing data from PARI/GP

4 messages · murfs, arun, jim holtman +1 more

#
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.
#
HI,

May be this helps:
Lines1<-"15,30,45;20,45,39;60,49,32;48,59,63"
res1<-read.table(text=unlist(strsplit(Lines1,split=";")),sep=",")
str(res1)
#'data.frame':??? 4 obs. of? 3 variables:
# $ V1: int? 15 20 60 48
# $ V2: int? 30 45 49 59
# $ V3: int? 45 39 32 63

#or
res2<-read.table(text=gsub(";","\n",Lines1),sep=",",stringsAsFactors=FALSE)
?res2
#? V1 V2 V3
#1 15 30 45
#2 20 45 39
#3 60 49 32
#4 48 59 63
identical(res1,res2)
#[1] TRUE
A.K.



----- Original Message -----
From: murfs <jm9461 at my.bristol.ac.uk>
To: r-help at r-project.org
Cc: 
Sent: Friday, December 21, 2012 12:39 PM
Subject: [R] Difficulty importing data from PARI/GP

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.
#
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:
[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"
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:

  
    
#
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