[R] Reading an upper triangular matrix

kjetil@entelnet.bo kjetil at entelnet.bo
Tue Nov 11 00:22:33 CET 2003


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




More information about the R-help mailing list