I have a table like the following: TABLE NO. 1 ID TIME 1325 0 1325 0 . . . . . . TABLE NO. 1 ID TIME 1325 0 1325 0 . . . . . . TABLE NO. 1 ID TIME 1325 0 1325 0 . . . . . . TABLE NO. 1 ID TIME 1325 0 1325 0 . . . . . . I used the following code: sim <- read.table("sim.tab", skip=1, as.is=T,header=T) it did not work, as there're rows with characters in between the data. Can anyone help me to read the table, while get rid of the character rows in between the data? thanks, -- View this message in context: http://r.789695.n4.nabble.com/can-not-read-a-table-tp4633971.html Sent from the R help mailing list archive at Nabble.com.
try this by reformatting the data and then reading back in:> x <- readLines(textConnection("TABLE NO. 1+ ID TIME + 1325 0 + 1325 0 + TABLE NO. 1 + ID TIME + 1325 0 + 1325 0 + TABLE NO. 1 + ID TIME + 1325 0 + 1325 0 + TABLE NO. 1 + ID TIME + 1325 0 + 1325 0"))> > # remove "TABLE" > x <- x[-grep("^TABLE", x)] > > # remove all but first "ID" > x <- x[-grep("^ID", x)[-1]] > > # now create temporary file with the data to re-read > fileName <- tempfile() > writeLines(x, fileName) > > # now read in the data > read.table(fileName, header = TRUE)ID TIME 1 1325 0 2 1325 0 3 1325 0 4 1325 0 5 1325 0 6 1325 0 7 1325 0 8 1325 0> >On Wed, Jun 20, 2012 at 10:08 AM, york8866 <yu_york at hotmail.com> wrote:> I have a table like the following: > > TABLE NO. ?1 > ID ? ? ?TIME > 1325 ? ?0 > 1325 ? ?0 > . ? ? ? . > . ? ? ? . > . ? ? ? . > TABLE NO. ?1 > ID ? ? ?TIME > 1325 ? ?0 > 1325 ? ?0 > . ? ? ? . > . ? ? ? . > . ? ? ? . > TABLE NO. ?1 > ID ? ? ?TIME > 1325 ? ?0 > 1325 ? ?0 > . ? ? ? . > . ? ? ? . > . ? ? ? . > TABLE NO. ?1 > ID ? ? ?TIME > 1325 ? ?0 > 1325 ? ?0 > . ? ? ? . > . ? ? ? . > . ? ? ? . > > I used the following code: > sim <- read.table("sim.tab", skip=1, as.is=T,header=T) > it did not work, as there're rows with characters in between the data. > Can anyone help me to read the table, while get rid of the character rows in > between the data? > thanks, > > -- > View this message in context: http://r.789695.n4.nabble.com/can-not-read-a-table-tp4633971.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.
Hi> > I have a table like the following: > > TABLE NO. 1 > ID TIME > 1325 0 > 1325 0 > . . > . . > . . > TABLE NO. 1 > ID TIME > 1325 0 > 1325 0 > . . > . . > . . > TABLE NO. 1 > ID TIME > 1325 0 > 1325 0 > . . > . . > . . > TABLE NO. 1 > ID TIME > 1325 0 > 1325 0 > . . > . . > . . > > I used the following code: > sim <- read.table("sim.tab", skip=1, as.is=T,header=T)I would read the file by readLines, get rid of lines containing "Table" and "ID" strings, and split the values to 2 columns Probably not the best approach but something like test<-readLines("clipboard") test<-test[-grep("TABLE", test)] test<-test[-grep("ID", test)] t(sapply(strsplit(test, "\t\t"), as.numeric)) [,1] [,2] [1,] 1325 0 [2,] 1325 0 [3,] 1325 0 [4,] 1325 0 can work You can transfer it to data frame easily. Regards Petr> it did not work, as there're rows with characters in between the data. > Can anyone help me to read the table, while get rid of the characterrows in> between the data? > thanks, > > -- > View this message in context:http://r.789695.n4.nabble.com/can-not-read-> a-table-tp4633971.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.