[R] reading in a tricky computer program output

Taka Matzmoto sell_mirage_ne at hotmail.com
Sun Feb 5 08:27:08 CET 2006


Hi R user

I need to read in some values from a computer program output.

I can't change the output format because the developer of the program 
doesn't allow to change the format of output.

There are two formats.

First one looks like this

if I have 10 variables,

------------------------------------------------------------------------------------------------------
       [ 1]              [2]           [3]              [4]            [5]
[ 1]  0.000
[ 2]  0.001         0.000
[ 3] -0.002         0.019         0.000
[ 4]  0.012        -0.004        -0.020         0.000
[ 5] -0.015         0.003         0.011         0.008         0.000
[ 6]  0.005        -0.008        -0.005         0.002         0.005
[ 7]  0.008        -0.007         0.013         0.003         0.007
[ 8] -0.014        -0.011        -0.010        -0.025         0.002
[ 9]  0.006         0.003        -0.010         0.002        -0.020
[10] 0.006         0.010        -0.006         0.005         0.008
[ 6]  0.000
[ 7] -0.037         0.000
[ 8]  0.010         0.027         0.000
[ 9]  0.032        -0.004         0.008         0.000
[10] -0.008        -0.011         0.015        -0.020         0.000

------------------------------------------------------------------------------------------------
NOTE: I put [number] to show that this output is similar to a lower diagonal 
matrix including diagonal. In an ouput there is no [number]


The second format looks like this
--------------------------------------------------------------------------------------
       [1]              [2]             [3]           [4]              [5]
[ 2] -0.002
[ 3]  0.003        -0.053
[ 4] -0.026         0.010         0.045
[ 5]  0.023        -0.008        -0.025        -0.016
[ 6] -0.012         0.023         0.013        -0.005        -0.011
[ 7] -0.031         0.031        -0.054        -0.013        -0.027
[ 8]  0.040         0.042         0.031         0.075        -0.007
[ 9] -0.012        -0.009         0.023        -0.005         0.037
[10] -0.013        -0.027         0.014        -0.013        -0.020
[ 7]  0.127
[ 8] -0.035        -0.166
[ 9] -0.083         0.015        -0.027
[10]  0.021         0.047        -0.052         0.048
---------------------------------------------------------------------------------------------------------
NOTE: I put [number] to show that this output is similar to a lower diagonal 
matrix without diagonal. In an ouput there is no [number]

The problem of this format is the fixed column length ( 5 columns)

To make matter worse, the number of variables keep changing (10, 20, 30, 40, 
50, 60,70,80,90, and 100) so I need to take into the number of variables 
when I write a R function to read in these numbers.

If the number of variables is 80, the output is very long.

I only came up with this tedious one.

First I read in the output using scan() and then make it a numeric vector

I created 10 character vectors. Creating a 100 variable character vector is 
the most boring things

I have ever done.

one of the character vectors that matchs with the first 10 variable output 
is

first.10<-c(
            "i.001.001",
            "i.002.001","i.002.002",
            "i.003.001","i.003.002","i.003.003",
            "i.004.001","i.004.002","i.004.003","i.004.004",
            "i.005.001","i.005.002","i.005.003","i.005.004","i.005.005",
            "i.006.001","i.006.002","i.006.003","i.006.004","i.006.005",
            "i.007.001","i.007.002","i.007.003","i.007.004","i.007.005",
            "i.008.001","i.008.002","i.008.003","i.008.004","i.008.005",
            "i.009.001","i.009.002","i.009.003","i.009.004","i.009.005",
            "i.010.001","i.010.002","i.010.003","i.010.004","i.010.005",
            "i.006.006",
            "i.007.006","i.007.007",
            "i.008.006","i.008.007","i.008.008",
            "i.009.006","i.009.007","i.009.008","i.009.009",
            "i.010.006","i.010.007","i.010.008","i.010.009","i.010.010"
           )

one of the character vectors that matchs with the second 10 variable output 
is

second.10<-c(
            "i.002.001",
            "i.003.001","i.003.002",
            "i.004.001","i.004.002","i.004.003",
            "i.005.001","i.005.002","i.005.003","i.005.004",
            "i.006.001","i.006.002","i.006.003","i.006.004","i.006.005",
            "i.007.001","i.007.002","i.007.003","i.007.004","i.007.005",
            "i.008.001","i.008.002","i.008.003","i.008.004","i.008.005",
            "i.009.001","i.009.002","i.009.003","i.009.004","i.009.005",
            "i.010.001","i.010.002","i.010.003","i.010.004","i.010.005",
            "i.007.006",
            "i.008.006","i.008.007",
            "i.009.006","i.009.007","i.009.008",
            "i.010.006","i.010.007","i.010.008","i.010.009"
           )

and then assign the character vector to the numeric vector by

names<-first.10
first.10 = numeric.vector
combined.one <- cbind(names,first.10)
container <- diag(10)
for (i in 1:(10*10))
    {
        k   <- as.numeric(substr(combined.one[i,1],7,9))
        l   <- as.numeric(substr(combined.one [i,1],3,5))
        val <- as.numeric(combined.one [i,2])
        container [k,l] <- val
    }

container <- t(container )

Is there any other neat way to do this?

Any help would be appreciated

TM




More information about the R-help mailing list