[R] Reading an upper triangular matrix

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 11 08:22:07 CET 2003


You supplied "what" as a list of length 2, which is not what you intended.
I presume you read every other item on the first line.

Try list("a", rep(list(0), 22-i))

I would have read the whole of the matrix as a character vector,
removed the items corresponding to the labels, converted to numeric.


On Mon, 10 Nov 2003 kjetil at entelnet.bo wrote:

> Hola!
> 
> I have data in the form of a symmetric distance matrix, in the file I 
> have recorded only the upper triangular part, with diagonal. The 
> matrix is 21x21, and the file have row and col names, and some other 
> information. I am trying to read with the following code (I tried 
> many variations on it, but all give the same error). The items in the 
> data file is delimited by white space. 
> 
> (Part of) script to read:
> 
> myfile <- file("Paises.dat", open="r")
>                # opens a connection which stays open until closed by 
> close(myfile)
> name <- readLines(con=myfile, n=1)
> varnames <- scan( myfile, what=character(0), nlines=1 )
>  
> stopifnot( length(varnames) == 21 )
> Paises <- matrix(0, 21, 21)
> colnames(Paises) <- varnames
> rownames(Paises) <- varnames
> for (i in 1:21) {
>     temp <- scan(myfile, what=list("a", rep(0,22-i) ), nlines=1, 
> sep="")
>     Paises[i, i:21] <- temp[[2]]
>   }
> 
> I get the following result:
> 
> > source("Paises.R", echo=TRUE)
> 
> > myfile <- file("Paises.dat", open = "r")
> 
> > name <- readLines(con = myfile, n = 1)
> 
> > varnames <- scan(myfile, what = character(0), nlines = 1)
> Read 21 items
> 
> > stopifnot(length(varnames) == 21)
> 
> > Paises <- matrix(0, 21, 21)
> 
> > colnames(Paises) <- varnames
> 
> > rownames(Paises) <- varnames
> 
> > for (i in 1:21) {
>     temp <- scan(myfile, what = list("a", rep(0, 22 - i)), nlines = 
> 1, 
>         sep = "")
>     Paises[i, i:21] <- temp[[2]]
> }
> Read 11 records
> Error in "[<-"(`*tmp*`, i, i:21, value = temp[[2]]) : 
>         number of items to replace is not a multiple of replacement 
> length
> > i
> [1] 1
> > temp
> [[1]]
>  [1] "Bolivia" "1"       "2"       "3"       "2"       "4"       "4"  
>     
>  [8] "6"       "6"       "8"       "8"      
> 
> [[2]]
>  [1] 0 2 3 2 3 5 5 6 6 7 8
> 
> > 
> 
> While I am asking only for one character string, multiple items are 
> read as strings! What is happening?
> 
> Kjetil Halvorsen
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list