# Re: [R] Help with matrix

From: jim holtman <jholtman_at_gmail.com>
Date: Fri, 25 Jul 2008 23:39:43 -0400

Is this what you want:

```+ 3   4  10
+ 2   1  30
```

> closeAllConnections()
> # create empty matrix
> mat <- matrix(0, max(x\$g1), max(x\$g2))
> mat[cbind(x[,1], x[,2])] <- x[,3]
> mat

[,1] [,2] [,3] [,4]

```[1,]    0    0    0    0
[2,]   30    0    0    0
[3,]    0    0    0   10
[4,]    0    0    0   50
```

>
>

On Fri, Jul 25, 2008 at 11:30 PM, Van Wyk, Jaap <jaapvw_at_uj.ac.za> wrote:
> Hi
> I have 3 vectors, say g1, g2 and f, where g1 and g2 contain the row and column indices of a matrix, with corresponding elements in f. How can I create a matrix containing the values in f ?
> For example:
> g1 g2 f
> 3 4 10
> 2 1 30
> 4 4 50
> How can then get a matrix that looks as follows:
> 0 0 0 0
> 30 0 0 0
> 0 0 0 10
> 0 0 0 50
>
> I am still learning R. I think reshape will help, but it is not clear how that works here.
>
> Any help is much appreciated.
> Thank you.
> Jacob
>
>
> Jacob L van Wyk
> Department of Statistics
> University of Johannesburg, APK
> Box 524
> Auckland Park 2006
> South Africa
> Office Tel: +27 11 559 3080
> Fax: +27 11 559 2832
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

```--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.
```
Received on Sat 26 Jul 2008 - 03:41:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 26 Jul 2008 - 05:32:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.