[R] Reading in data in a triangle

John Fox jfox at mcmaster.ca
Sat Oct 6 06:37:32 CEST 2001


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list