G'day Taka,
>>>>> "TM" == Taka Matzmoto <[EMAIL PROTECTED]> writes:
TM> and then assign the character vector to the numeric vector by
TM> names<-first.10
TM> first.10 = numeric.vector
TM> combined.one <- cbind(names,first.10)
TM> container <- diag(10)
TM> for (i in 1:(10*10))
I don't really understand this loop. If I reverse-engineer this code
correctly thenthe matrix `combined.one' is not a 2*100 matrix, so you
should get an error while exectuting this loop.
TM> Is there any other neat way to do this?
Neat way to create those character vectors? Or a neat way to read in
the data from a file?
If the latter, I would use the following code:
matzmoto <- function(file, diag=TRUE){
dat <- scan(file)
if(diag){
nvar <- sqrt(2*length(dat)+0.25) - 0.5
nn <- nvar
}else{
nvar <- sqrt(2*length(dat)+0.25) + 0.5
nn <- nvar - 1
}
res <- matrix(0,nvar,nvar)
ind <- upper.tri(res, diag=diag)
rind <- 1:5
while(nn > 0){
if( nn < 5 ){
rind <- rind[1:nn]
tmp <- matrix(0,nn,nvar)
}else{
tmp <- matrix(0,5,nvar)
}
how.many <- sum(ind[rind,])
tmp[ind[rind,]] <- dat[1:how.many]
res[rind,] <- tmp
dat <- dat[-(1:how.many)]
rind <- rind + 5
nn <- nn - 5
}
t(res)
}
res <- matzmoto("matzmoto1.dat", TRUE)
print(res)
res <- matzmoto("matzmoto2.dat", FALSE)
print(res)
After storing the two examples that you posted into the files
matzmoto1.dat and matzmoto2.dat, respectively, and removing the part
that you said you have added, I get the following result on my machine
when sourcing the above code:
> source("matzmoto.R")
Read 55 items
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.00 0
[2,] 0.001 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.00 0
[3,] -0.002 0.019 0.000 0.000 0.000 0.000 0.000 0.000 0.00 0
[4,] 0.012 -0.004 -0.020 0.000 0.000 0.000 0.000 0.000 0.00 0
[5,] -0.015 0.003 0.011 0.008 0.000 0.000 0.000 0.000 0.00 0
[6,] 0.005 -0.008 -0.005 0.002 0.005 0.000 0.000 0.000 0.00 0
[7,] 0.008 -0.007 0.013 0.003 0.007 -0.037 0.000 0.000 0.00 0
[8,] -0.014 -0.011 -0.010 -0.025 0.002 0.010 0.027 0.000 0.00 0
[9,] 0.006 0.003 -0.010 0.002 -0.020 0.032 -0.004 0.008 0.00 0
[10,] 0.006 0.010 -0.006 0.005 0.008 -0.008 -0.011 0.015 -0.02 0
Read 45 items
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0
[2,] -0.002 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0
[3,] 0.003 -0.053 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0
[4,] -0.026 0.010 0.045 0.000 0.000 0.000 0.000 0.000 0.000 0
[5,] 0.023 -0.008 -0.025 -0.016 0.000 0.000 0.000 0.000 0.000 0
[6,] -0.012 0.023 0.013 -0.005 -0.011 0.000 0.000 0.000 0.000 0
[7,] -0.031 0.031 -0.054 -0.013 -0.027 0.127 0.000 0.000 0.000 0
[8,] 0.040 0.042 0.031 0.075 -0.007 -0.035 -0.166 0.000 0.000 0
[9,] -0.012 -0.009 0.023 -0.005 0.037 -0.083 0.015 -0.027 0.000 0
[10,] -0.013 -0.027 0.014 -0.013 -0.020 0.021 0.047 -0.052 0.048 0
The documentation of the function is quite simple. Just pass the name
of the file in which the output is and whether it is a file that
includes the output of the diagonal or not. If you don't trust the
calculation of how many variables are involved, then you might want to
change the function so that this is another input paramter.
HTH.
Cheers,
Berwin
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html