the following the the lower.tri matrix in a file named luxry.car and i want to read it in R as a lower.tri matrix.how can i do? i have try to use help.search("read"),but no result what i want. 1.000 0.591 1.000 0.356 0.350 1.000 -0.098 0.072 0.380 1.000 0.573 0.408 0.382 0.062 1.000 0.156 0.232 0.517 0.424 0.303 1.000 0.400 0.414 0.611 0.320 0.401 0.479 1.000 0.282 0.375 0.512 0.346 0.308 0.463 0.605 1.000 0.519 0.484 0.467 0.167 0.455 0.311 0.574 0.557 1.000
How about the following: > a <- scan("clipboard") Read 45 items > n2 <- length(a) > n <- (-1+sqrt(1+8*n2))/2 > A <- array(NA, dim=c(n,n)) > A[lower.tri(A,diag=TRUE)] <- a > A [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1.000 NA NA NA NA NA NA NA NA [2,] 0.591 1.000 NA NA NA NA NA NA NA [3,] 1.000 0.573 0.517 NA NA NA NA NA NA [4,] 0.356 0.408 0.424 0.320 NA NA NA NA NA [5,] 0.350 0.382 0.303 0.401 0.512 NA NA NA NA [6,] 1.000 0.062 1.000 0.479 0.346 1.000 NA NA NA [7,] -0.098 1.000 0.400 1.000 0.308 0.519 0.167 NA NA [8,] 0.072 0.156 0.414 0.282 0.463 0.484 0.455 0.574 NA [9,] 0.380 0.232 0.611 0.375 0.605 0.467 0.311 0.557 1 hope this helps. spencer graves rongguiwong wrote:>the following the the lower.tri matrix in a file named luxry.car >and i want to read it in R as a lower.tri matrix.how can i do? >i have try to use help.search("read"),but no result what i want. > > > 1.000 > 0.591 1.000 > 0.356 0.350 1.000 > -0.098 0.072 0.380 1.000 > 0.573 0.408 0.382 0.062 1.000 > 0.156 0.232 0.517 0.424 0.303 1.000 > 0.400 0.414 0.611 0.320 0.401 0.479 1.000 > 0.282 0.375 0.512 0.346 0.308 0.463 0.605 1.000 > 0.519 0.484 0.467 0.167 0.455 0.311 0.574 0.557 1.000 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567
rongguiwong <0034058 at fudan.edu.cn> writes:> the following the the lower.tri matrix in a file named luxry.car > and i want to read it in R as a lower.tri matrix.how can i do? > i have try to use help.search("read"),but no result what i want.Here's one way:> x <- scan()1: 1.000 2: 0.591 1.000 4: 0.356 0.350 1.000 7: -0.098 0.072 0.380 1.000 11: 0.573 0.408 0.382 0.062 1.000 16: 0.156 0.232 0.517 0.424 0.303 1.000 22: 0.400 0.414 0.611 0.320 0.401 0.479 1.000 29: 0.282 0.375 0.512 0.346 0.308 0.463 0.605 1.000 37: 0.519 0.484 0.467 0.167 0.455 0.311 0.574 0.557 1.000 46: Read 45 items> m <- matrix(0,9,9) > m[upper.tri(m,diag=TRUE)] <- x # Yes, upper! > m <- t(m) > m[upper.tri(m,diag=TRUE)] <- x(Assuming that you really want a symmetric matrix. If you want just the lower triangle, simply omit the last line.) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
rongguiwong <0034058 <at> fudan.edu.cn> writes: : the following the the lower.tri matrix in a file named luxry.car : and i want to read it in R as a lower.tri matrix.how can i do? : i have try to use help.search("read"),but no result what i want. : : 1.000 : 0.591 1.000 : 0.356 0.350 1.000 : -0.098 0.072 0.380 1.000 : 0.573 0.408 0.382 0.062 1.000 : 0.156 0.232 0.517 0.424 0.303 1.000 : 0.400 0.414 0.611 0.320 0.401 0.479 1.000 : 0.282 0.375 0.512 0.346 0.308 0.463 0.605 1.000 : 0.519 0.484 0.467 0.167 0.455 0.311 0.574 0.557 1.000 Here is one way: data.matrix(read.table(file.dat, fill = TRUE, col.names = 1:9)) If you don't know that that its 9x9 in advance then try this: n <- max(count.fields(file.dat)) data.matrix(read.table(file.dat, fill = TRUE, col.names = 1:n))