[R-SIG-Finance] Populating ONLY some places of a matrix

Jorge Nieves jorge.nieves at moorecap.com
Thu Oct 2 21:08:42 CEST 2008


It works fine thanks a lot !!! 

-----Original Message-----
From: Patrick Burns [mailto:patrick at burns-stat.com] 
Sent: Thursday, October 02, 2008 02:52 PM
To: Jorge Nieves
Cc: R-sig-finance
Subject: Re: [R-SIG-Finance] Populating ONLY some places of a matrix

Use a matrix as a subscript.

tenByTen[ dataset[, 1:2] ] <- dataset[, 'x']

This assumes 'dataset' is a matrix and not a data frame.

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Jorge Nieves wrote:
> Hi
>
> I have a 10 by 10 matrix initialized with zeros (10 x10 = 100), see 
> bellow. I also have  values in a data set for a variable x, which I 
> would like to use to populate the 10 by 10 matrix. The total number 
> known values for  x is less that the total 100. The data set also has 
> the corresponding row and column where each value of x should be 
> placed in the 10 by 10  matrix. When a x value does not exist, it 
> should remain zero in the 10 by 10.
>
> I was able to place the data from the data set in the 10 by 10 via a 
> for loop. I was wondering If there is a more efficient method for 
> doing this operation, maybe some R function?
>
> Any tips are highly appreciated.
>
> Jorge
>  
>
>     s1 s2 s3 s4 s5 s6 s7 s8 s9 s10
> l1   0  0  0  0  0  0  0  0  0   0
> l2   0  0  0  0  0  0  0  0  0   0
> l3   0  0  0  0  0  0  0  0  0   0
> l4   0  0  0  0  0  0  0  0  0   0
> l5   0  0  0  0  0  0  0  0  0   0
> l6   0  0  0  0  0  0  0  0  0   0
> l7   0  0  0  0  0  0  0  0  0   0
> l8   0  0  0  0  0  0  0  0  0   0
> l9   0  0  0  0  0  0  0  0  0   0
> l10  0  0  0  0  0  0  0  0  0   0
>
> Data set
>
>     row   column        x
> 1   2  1 -0.668187861
> 2   3 1 -0.908980612
> 3   4 1 -0.580909134
> 4   5 1 -0.551092216
> 5   6 1 -1.079977605
> 6   7 1 -0.969920140
> 7   8 1 -0.612563647
> 8   9 1 -0.957372449
> 9  10 1 -0.747838475
> 10  3 2  0.000000000
> 11  4 2 -0.573238323
> 12  5 2 -1.010567198
> 13  6 2 -0.997079195
> 14  7 2 -0.841461751
> 15  8 2 -0.724916037
> 16  9 2 -0.629846393
> 17 10 2 -0.474943784
> 18  4 3  0.000000000
> 19  5 3  0.000000000
> 20  6 3 -0.392330001
> 21  7 3 -0.645115545
> 22  8 3 -0.132228425
> 23  9 3 -0.266551015
> 24 10 3 -0.242061441
> 25  5 4  0.000000000
> 26  6 4  0.000000000
> 27  7 4 -1.706986344
> 28  8 4 -0.615059694
> 29  9 4 -0.090352204
> 30 10 4  0.094375693
> 31  6 5  0.000000000
> 32  7 5  0.000000000
> 33  8 5  0.000000000
> 34  9 5  0.295278995
> 35 10 5  0.008334823
> 36  7 6  0.000000000
> 37  8 6  0.000000000
> 38  9 6  0.000000000
> 39 10 6  0.000000000
> 40  8 7  0.000000000
> 41  9 7  0.000000000
> 42 10 7  0.000000000
> 43  9 8  0.000000000
> 44 10 8  0.000000000
> 45 10 9  0.000000000
>
> Jorge Nieves
>
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
>
>   



More information about the R-SIG-Finance mailing list