Skip to content

Change the result data

4 messages · Muhammad Subianto, Philipp Pagel, vito muggeo +1 more

#
Dear R-helper,

I have a data like:

 > hec.data <-array(c(5,15,20,68,29,54,84,119,14,14,17,26,16,10,94,7),
+            dim=c(4,4),
+            dimnames=list(eye=c("Green","Hazel", "Blue", "Brown"),
+            hair=c("Black", "Brown", "Red", "Blond")))
 > hec.data
       hair
eye     Black Brown Red Blond
  Green     5    29  14    16
  Hazel    15    54  14    10
  Blue     20    84  17    94
  Brown    68   119  26     7
 >

but I want the result like below.:

hair   eye counts
Black Green    5
Black Hazel   15
Black  Blue   20
Black Brown   68
Brown Green   29
Brown Hazel   54
Brown  Blue   84
Brown Brown  119
Red Green     14
Red Hazel     14
Red  Blue     17
Red Brown     26
Blond Green   16
Blond Hazel   10
Blond  Blue   94
Blond Brown    7

How can I do it. Thanks you for your help.

Best regards,
Muhammad Subianto
#
Using stack() would be a possibility:
values   ind   eye
1       5 Black Green
2      15 Black Hazel
3      20 Black  Blue
4      68 Black Brown
5      29 Brown Green
6      54 Brown Hazel
7      84 Brown  Blue
8     119 Brown Brown
9      14   Red Green
10     14   Red Hazel
11     17   Red  Blue
12     26   Red Brown
13     16 Blond Green
14     10 Blond Hazel
15     94 Blond  Blue
16      7 Blond Brown


Also have a look at reshape().

cu
	Philipp
#
as.vector is a possible, simple solution
Also use rep() on dimnames(hec.data)[[1]] to get the names vector with
correct length
[,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
[1]  1  4  7 10 13  2  5  8 11 14  3  6  9 12 15


hope this helps,
vito


----- Original Message -----
From: Muhammad Subianto <subianto at cs.uu.nl>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, February 27, 2004 12:28 PM
Subject: [R] Change the result data
http://www.R-project.org/posting-guide.html
#
Dear Muhammad,

One approach is:

	class(hec.data) <- "table"
	as.data.frame(hec.data)

I hope that this helps,
 John