[R] Text file: multiple matrix

Rui Barradas ruipbarradas at sapo.pt
Tue Oct 9 17:21:21 CEST 2012


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.




More information about the R-help mailing list