Re: [R] Odp: Data frame with 3 columns to matrix

From: Michael Bach <phaebz_at_gmail.com>
Date: Tue, 19 Apr 2011 15:21:13 +0300

Petr PIKAL <petr.pikal_at_precheza.cz> writes:

> Hi
>
> r-help-bounces@r-project.org napsal dne 19.04.2011 09:46:47:
>
>> Dear R Users,
>>
>> Lets assume I have this data frame:
>>
>> x y z
>> 1 1.00 5 0.5
>> 2 1.02 5 0.7
>> 3 1.04 7 0.1
>> 4 1.06 9 0.4
>>
>> x and y columns are sorted and the values not necessarily integers. z
>> values are not sorted. Now I would like to create a matrix out of this
>> with x as first column values and y as first row values. Matrix element

>> a_11 shall be left NA. The a_ij should have the z value for the
>> corresponding x and y pair. The result shall be some sort of a grid and
>> then e.g. look like:
>>
>> [,1] [,2] [,3] [,4] [,5]
>> [1,] NA 5 6 7 9 (y)
>> [2,] 1.00 0.5 NA NA NA
>> [3,] 1.02 0.7 NA NA NA
>> [4,] 1.04 NA NA 0.1 NA
>> [5,] 1.06 NA NA NA 0.4
>> (x)
>>
>> This example is just for illustration. The resulting matrix should have
>> more numeric values than NA's.
>
> I am not sure if this is the solution you want
>
> tab<-xtabs(z~x+y, data=df)
> tab[tab==0]<-NA
>

This looks right, but the resulting table does not have rows == columns and there are also no missing values (0 or NA) which should be there. I think I did not make myself clear, please see also my reply to the previous answer to my question by David Winsemius.

Thanks for your reply though!



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 19 Apr 2011 - 12:24:20 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 Tue 19 Apr 2011 - 14:20:32 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.

list of date sections of archive