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:> 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.dfV1 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.