Hi there! I'm a newbie in R This is my problem: I have a txt file composed by 100 matrix (256x256) separated by a blank line! How can I save automatically the matrix in separated txt file (100)? e.g. 1? matrix from line 1 to line 256 257 blank line 2?matrix from line 258 to line 513 514 blank line 3? matrix from line 515 to line 770 771 blank line 4? matrix from line 772 to line 1027...... Thanks -- View this message in context: http://r.789695.n4.nabble.com/Text-file-multiple-matrix-tp4645551.html Sent from the R help mailing list archive at Nabble.com.
Hello, Try the following, substituting your filename for "test.txt". fun <- function(filenumber, con, n, sep = " ", prefix = "RTest"){ txt <- readLines(con, n = n) tc <- textConnection(txt) on.exit(close(tc)) tbl <- read.table(tc, sep = sep)[-n, ] filename <- sprintf("%s_%03d", prefix, filenumber) write.table(tbl, filename, row.names = FALSE) filename } NRows <- 256 NMat <- 100 fc <- file("test.txt", open = "rb") lapply(seq_len(NMat), fun, fc, n = NRows + 1) close(fc) Hope this helps, Rui Barradas Em 09-10-2012 11:15, ludovico escreveu:> Hi there! I'm a newbie in R > This is my problem: I have a txt file composed by 100 matrix (256x256) > separated by a blank line! How can I save automatically the matrix in > separated txt file (100)? > e.g. > 1? matrix from line 1 to line 256 > 257 blank line > 2?matrix from line 258 to line 513 > 514 blank line > 3? matrix from line 515 to line 770 > 771 blank line > 4? matrix from line 772 to line 1027...... > Thanks > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Text-file-multiple-matrix-tp4645551.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.
Hi, You can try this and see. I'm assuming that the initial text file named "test.txt". x<-read.table("test.txt",header=T) # if headers are present in "test.txt" or x<-read.table("test.txt") # Actually, read.table() command skips the blank lines. n<-256 for (i in 1:100){ filename=paste("file_",i,".txt",sep="") m<-x[((i-1)*256+1):(i*256),] write.table(m,filename,row.names=F,col.names=F) } Regards, Purna On 10/9/12, ludovico <ludovicofrate at hotmail.it> wrote:> Hi there! I'm a newbie in R > This is my problem: I have a txt file composed by 100 matrix (256x256) > separated by a blank line! How can I save automatically the matrix in > separated txt file (100)? > e.g. > 1? matrix from line 1 to line 256 > 257 blank line > 2?matrix from line 258 to line 513 > 514 blank line > 3? matrix from line 515 to line 770 > 771 blank line > 4? matrix from line 772 to line 1027...... > Thanks > > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Text-file-multiple-matrix-tp4645551.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. >