Dear all, How to read a row dataset one by one and then print it. x1 x2 x3 x4 x5 y a b a c c M1 c b b c c M4 c c a c c M2 c a c a a M2 c c a a a M1 c a b c a M3 c c a b c M3 c a c a b M2 c c a b a M1 I need a result like read row no 1, [1] a b a c c M1 read row no 2, [1] c b b c c M4 . . . the last row, [1] c c a b a M1 Kind regards, Jan Sabee
use a loop associated with the scan function. for (i in 1:9) { print(scan(file="c:/a.txt",sep="\t",skip=i,nlines=1,fill=T,quiet=T,what="raw")) } this works but there has to be a better solution Jan Sabee wrote:> > Dear all, > How to read a row dataset one by one and then print it. > > x1 x2 x3 x4 x5 y > a b a c c M1 > c b b c c M4 > c c a c c M2 > c a c a a M2 > c c a a a M1 > c a b c a M3 > c c a b c M3 > c a c a b M2 > c c a b a M1 > > I need a result like > read row no 1, > [1] a b a c c M1 > read row no 2, > [1] c b b c c M4 > . > . > . > the last row, > [1] c c a b a M1 > > Kind regards, > Jan Sabee > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open a connection a read line by line from that one, e.g. myReadPrint <- function(pathname, ...) { con <- file(pathname, open="r") on.exit(close(con)) # Guarantees to close connection! count <- 0; while(TRUE) { line <- scan(con, sep="\t", nlines=1, fill=TRUE, quiet=TRUE, what="raw"); # Alternatively, just... # line <- readLines(con, n=1) if (length(line) == 0) break; count <- count + 1; cat("read row no ", count, ",\n", sep=""); print(line); } } See ?file for more details. /Henrik Clark Allan wrote:> use a loop associated with the scan function. > > for (i in 1:9) > { > > print(scan(file="c:/a.txt",sep="\t",skip=i,nlines=1,fill=T,quiet=T,what="raw")) > } > > > this works but there has to be a better solution > > > > Jan Sabee wrote: > >>Dear all, >>How to read a row dataset one by one and then print it. >> >>x1 x2 x3 x4 x5 y >>a b a c c M1 >>c b b c c M4 >>c c a c c M2 >>c a c a a M2 >>c c a a a M1 >>c a b c a M3 >>c c a b c M3 >>c a c a b M2 >>c c a b a M1 >> >>I need a result like >>read row no 1, >>[1] a b a c c M1 >>read row no 2, >>[1] c b b c c M4 >>. >>. >>. >>the last row, >>[1] c c a b a M1 >> >>Kind regards, >>Jan Sabee >> >>______________________________________________ >>R-help at stat.math.ethz.ch mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >> >> >>------------------------------------------------------------------------ >> >>______________________________________________ >>R-help at stat.math.ethz.ch mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
For Henrik and Clark, thanks for your help. Then If I load to dataframe, MM16 <- read.table("G:\\Stuff\\data\\MM16.txt") MM16 x1 x2 x3 x4 x5 y a b a c c M1 c b b c c M4 c c a c c M2 c a c a a M2 c c a a a M1 c a b c a M3 c c a b c M3 c a c a b M2 c c a b a M1 How can I do it. Thanks again for your help. Jan Sabee On 6/10/05, Henrik Bengtsson <hb at maths.lth.se> wrote:> Open a connection a read line by line from that one, e.g. > > myReadPrint <- function(pathname, ...) { > con <- file(pathname, open="r") > on.exit(close(con)) # Guarantees to close connection! > > count <- 0; > while(TRUE) { > line <- scan(con, sep="\t", nlines=1, fill=TRUE, > quiet=TRUE, what="raw"); > # Alternatively, just... > # line <- readLines(con, n=1) > if (length(line) == 0) > break; > count <- count + 1; > cat("read row no ", count, ",\n", sep=""); > print(line); > } > } > > See ?file for more details. > > /Henrik > > Clark Allan wrote: > > use a loop associated with the scan function. > > > > for (i in 1:9) > > { > > > > print(scan(file="c:/a.txt",sep="\t",skip=i,nlines=1,fill=T,quiet=T,what="raw")) > > } > > > > > > this works but there has to be a better solution > > > > > > > > Jan Sabee wrote: > > > >>Dear all, > >>How to read a row dataset one by one and then print it. > >> > >>x1 x2 x3 x4 x5 y > >>a b a c c M1 > >>c b b c c M4 > >>c c a c c M2 > >>c a c a a M2 > >>c c a a a M1 > >>c a b c a M3 > >>c c a b c M3 > >>c a c a b M2 > >>c c a b a M1 > >> > >>I need a result like > >>read row no 1, > >>[1] a b a c c M1 > >>read row no 2, > >>[1] c b b c c M4 > >>. > >>. > >>. > >>the last row, > >>[1] c c a b a M1 > >> > >>Kind regards, > >>Jan Sabee > >> > >>______________________________________________ > >>R-help at stat.math.ethz.ch mailing list > >>https://stat.ethz.ch/mailman/listinfo/r-help > >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >> > >> > >>------------------------------------------------------------------------ > >> > >>______________________________________________ > >>R-help at stat.math.ethz.ch mailing list > >>https://stat.ethz.ch/mailman/listinfo/r-help > >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
You could use by(dat,row.names(dat), print) #It will return a list with results for each row Is this what you want? Cheers Francisco>From: Jan Sabee <jan.sabee at gmail.com> >Reply-To: Jan Sabee <jan.sabee at gmail.com> >To: R-help at stat.math.ethz.ch >Subject: [R] How to read a row dataset one by one >Date: Fri, 10 Jun 2005 10:32:15 +0200 > >Dear all, >How to read a row dataset one by one and then print it. > >x1 x2 x3 x4 x5 y >a b a c c M1 >c b b c c M4 >c c a c c M2 >c a c a a M2 >c c a a a M1 >c a b c a M3 >c c a b c M3 >c a c a b M2 >c c a b a M1 > >I need a result like >read row no 1, >[1] a b a c c M1 >read row no 2, >[1] c b b c c M4 >. >. >. >the last row, >[1] c c a b a M1 > >Kind regards, >Jan Sabee > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html