I have plain-text data in lower triangular form that I want to read in. Does anyone know of an easy way to do this? ______________________________________________________________________ Stuart Luppescu -=-=- University of Chicago $(B:MJ8$HCRF`H~$NIc(B -=-=- s-luppescu at uchicago.edu http://www.consortium-chicago.org/people/sl.html http://musuko.uchicago.edu/pubkey.asc for PGP Public Key ICQ #21172047 AIM: psycho7070 I have found little that is good about human beings. In my experience most of them are trash. -- Sigmund Freud>> Sent on 05-Oct-2001 at 15:14:29 with xfmail-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Stuart, At 03:16 PM 05/10/2001 -0500, s-luppescu at uchicago.edu wrote:>I have plain-text data in lower triangular form that I want to read in. Does >anyone know of an easy way to do this?It's not altogether clear to me what you want to do, but I'll take a stab at it. Simply to read the data into a vector is no trick: scan will do that. Assuming that you want to read the data into a lower-triangular matrix, you might try something like the following, starting with the file LT.txt, which contains: 1 2 3 4 5 6 7 8 9 10 Then, constructing an upper-triangular matrix and transposing it, > U <- matrix(0, 4, 4) > U[outer(1:4, 1:4, '<=')] <- scan('c:/temp/LT.txt') Read 10 items > t(U) [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 3 0 0 [3,] 4 5 6 0 [4,] 7 8 9 10 Is that what you need? John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Dear Stuart, > > At 03:16 PM 05/10/2001 -0500, s-luppescu at uchicago.edu wrote: > >I have plain-text data in lower triangular form that I want > to read in. Does > >anyone know of an easy way to do this? > > It's not altogether clear to me what you want to do, but I'll > take a stab > at it. > > Simply to read the data into a vector is no trick: scan will do that. > Assuming that you want to read the data into a > lower-triangular matrix, you > might try something like the following, starting with the > file LT.txt, > which contains: > > 1 > 2 3 > 4 5 6 > 7 8 9 10 > > Then, constructing an upper-triangular matrix and transposing it, > > > U <- matrix(0, 4, 4) > > U[outer(1:4, 1:4, '<=')] <- scan('c:/temp/LT.txt') > Read 10 items > > t(U) > [,1] [,2] [,3] [,4] > [1,] 1 0 0 0 > [2,] 2 3 0 0 > [3,] 4 5 6 0 > [4,] 7 8 9 10 > > Is that what you need? > > John >John's solution requires the dimension of the matrix be known beforehand. Here's a slightly more general way, using connections. Maybe others can improve on it? lucon <- file("LU.txt", open="rt") x <- readLines(lucon) close(lucon) n <- length(x) U <- matrix(0, n, n) U[upper.tri(U, diag=TRUE)] <- as.numeric(unlist(strsplit(x, " "))) U <- t(U) Andy -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._