Re: [R] reading in a tricky computer program output

From: jim holtman <jholtman_at_gmail.com>
Date: Mon 06 Feb 2006 - 14:13:31 EST

Is this what you want? You can use 'scan' to read in and 'fill' out data in a row.

> x <- scan('/temp/document1.txt', what=list(0, 0, 0, 0, 0), fill=T,
multi.line=F)
Read 15 records
> x <- do.call('rbind', x) # create a matrix
> x

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 0 0.001 -0.002 0.012 -0.015 0.005 0.008 -0.014 0.006 0.006 0 -0.037 0.010 0.032

[2,]   NA 0.000  0.019 -0.004  0.003 -0.008 -0.007 -0.011  0.003  0.010
NA  0.000 0.027 -0.004
[3,]   NA    NA  0.000 -0.020  0.011 -0.005  0.013 -0.010 -0.010 -0.006
NA     NA 0.000  0.008
[4,]   NA    NA     NA  0.000  0.008  0.002  0.003 -0.025  0.002  0.005
NA     NA    NA  0.000
[5,]   NA    NA     NA     NA  0.000  0.005  0.007  0.002 -0.020  0.008
NA     NA    NA     NA
      [,15]
[1,] -0.008
[2,] -0.011
[3,]  0.015

[4,] -0.020
[5,] 0.000
> t(x) # transpose to get in the order you want
        [,1]   [,2]   [,3]   [,4]   [,5]
 [1,]  0.000     NA     NA     NA     NA
 [2,]  0.001  0.000     NA     NA     NA
 [3,] -0.002  0.019  0.000     NA     NA
 [4,]  0.012 -0.004 -0.020  0.000     NA
 [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
[11,]  0.000     NA     NA     NA     NA
[12,] -0.037  0.000     NA     NA     NA
[13,]  0.010  0.027  0.000     NA     NA
[14,]  0.032 -0.004  0.008  0.000     NA
[15,] -0.008 -0.011 0.015 -0.020 0.000
>

On 2/5/06, Taka Matzmoto <sell_mirage_ne@hotmail.com> wrote:
>
> 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
>
> ______________________________________________
> R-help@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
>

--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help@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
Received on Mon Feb 06 14:22:54 2006

This archive was generated by hypermail 2.1.8 : Tue 07 Feb 2006 - 00:53:06 EST