# [R] transforming a data frame to matrix

David Winsemius dwinsemius at comcast.net
Sat Feb 18 14:56:26 CET 2012

```On Feb 18, 2012, at 8:35 AM, Adel ESSAFI wrote:

> with simpler expressions:
> if I have a dataframe
> A B C
> is it possible de transform it to a matrix where nrow(A) colomns,
> nrow(B)
> row and for each value of C, we put the corresponding value in the
> matrix.
>

Perhaps:

xtabs(C ~ A + B, data=dfrm)

There will be an implicit summation of C-values in the presence of
duplicates. For the application of other functions, you would use
tapply. This untested code might give you counts:

with(dfrm, tapply(C, list(A, B), length)

--
David.

> Regards
>
>
>
> Le 18 février 2012 13:57, Adel ESSAFI <adelessafi at gmail.com> a écrit :
>
>> Hello

I had not planned on answering this question, because the data was too
ambiguous. You should learn to use dput when posting data with your
questions.

>>
>>> orderulcount
>>   Group.1 Group.2 Group.3  x    V5
>> 7        C       L     0.0 30 C / L
>> 19       C       L     0.2 27 C / L
>> 31       C       L     0.4 15 C / L
>> 43       C       L     0.6  7 C / L
>> 54       C       L     0.8  2 C / L
>> 10       C       S     0.0 27 C / S
>> 22       C       S     0.2 10 C / S
>> 34       C       S     0.4  6 C / S
>> 46       C       S     0.6  1 C / S
>> 1        D       D     0.0 30 D / D
>> 13       D       D     0.2 30 D / D
>> 25       D       D     0.4 17 D / D
>> 37       D       D     0.6  9 D / D
>> 49       D       D     0.8  2 D / D
>> 3        D       F     0.0 30 D / F
>> 15       D       F     0.2 26 D / F
>> 27       D       F     0.4 13 D / F
>> 39       D       F     0.6  3 D / F
>> 51       D       F     0.8  3 D / F
>> 5        D       I     0.0 26 D / I
>> 17       D       I     0.2  7 D / I
>> 29       D       I     0.4  7 D / I
>> 41       D       I     0.6  1 D / I
>> 53       D       I     0.8  1 D / I
>> 8        D       L     0.0 30 D / L
>> 20       D       L     0.2 26 D / L
>> 32       D       L     0.4 14 D / L
>> 44       D       L     0.6  8 D / L
>> 55       D       L     0.8  2 D / L
>> 11       D       S     0.0 27 D / S
>> 23       D       S     0.2  8 D / S
>> 35       D       S     0.4  4 D / S
>> 47       D       S     0.6  1 D / S
>> 2        I       D     0.0 30 I / D
>> 14       I       D     0.2 30 I / D
>> 26       I       D     0.4 14 I / D
>> 38       I       D     0.6  9 I / D
>> 50       I       D     0.8  2 I / D
>> 4        I       F     0.0 30 I / F
>> 16       I       F     0.2 29 I / F
>> 28       I       F     0.4 16 I / F
>> 40       I       F     0.6  5 I / F
>> 52       I       F     0.8  2 I / F
>> 6        I       I     0.0 30 I / I
>> 18       I       I     0.2 17 I / I
>> 30       I       I     0.4  4 I / I
>> 42       I       I     0.6  1 I / I
>> 9        I       L     0.0 30 I / L
>> 21       I       L     0.2 27 I / L
>> 33       I       L     0.4 17 I / L
>> 45       I       L     0.6  8 I / L
>> 56       I       L     0.8  2 I / L
>> 12       I       S     0.0 28 I / S
>> 24       I       S     0.2 11 I / S
>> 36       I       S     0.4  6 I / S
>> 48       I       S     0.6  3 I / S
>>
>> I have a data frame formed by 5 colomns. How to transform this data
>> frame
>> to a matrix formed as follow:
>> 1. the colomn 5 is the comumn (string)
>> 2.  the colomn 5 row
>> 3. the data are the colomn x.
>>
>> I aim to draw barplot later using the matrix.
>>
>> Thanks for any help
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> --
>> PhD candidate in Computer Science
>> 3 avenue lamine, cité ezzahra, Sousse 4000
>> Tunisia
>> tel: +216 97 246 706 (+33640302046 jusqu'au 15/6)
>> fax: +216 71 391 166
>>
>
>
>
> --
> PhD candidate in Computer Science
> 3 avenue lamine, cité ezzahra, Sousse 4000
> Tunisia
> tel: +216 97 246 706 (+33640302046 jusqu'au 15/6)
> fax: +216 71 391 166
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help