[R] Help with tables
Alejandro Rodríguez
rodrigueza at schwabe.com.mx
Mon Dec 3 13:55:55 CET 2007
Hello everyone. I want to thank you for your response.
I tried this on weekend to attack my problem:
1) Turned into factors all my variables to tabulate:
infec1<-bamas$P2_A
infec2<-bamas$P2_B
infec3<-bamas$P2_C
aglomera<-bamas$QCL_1
Where bamas is my data frame, P2_A, P2_B and P2_C the infections and QCL_1
the clusters.
2) Defined a table for each one of the variables I wanted:
t1<-table(infec1,aglomera)
t2<-table(infec2,aglomera)
t3<-table(infec3,aglomera)
3) Tables generated above are of n1 x 4, n2 x 4, n3 x 4 where n1, n2 and n3
are different and so the dimmensions of my matrices, then if I want to sum
the three tables R showed me the error that they were of
different dimmensions, so I decided to make them of the same
dimmensions by "filling the holes" (missing rows) with zeros of each row in
each table in order to transform them into a matrix of N x 4:
For example, comparing my table t1 with t2, I could see that, in table
1 there where missing rows 7, 10, 12 and 13, and in the case of t3 rows 7
and 12 ;
t1
aglomera
infec1 1 2 3 4
1 117 88 76 83
2 10 10 9 7
3 15 11 14 14
4 2 0 1 1
5 2 3 1 0
6 1 0 1 0
8 3 3 0 1
9 3 1 1 0
11 0 0 1 1
t3
aglomera
infec2 1 2 3 4
1 12 9 6 6
2 37 45 33 37
3 11 11 8 11
4 3 0 0 2
5 2 1 0 0
6 0 0 0 1
8 3 1 2 2
9 2 2 0 2
10 1 1 2 1
11 1 0 0 0
13 0 0 1 0
Then I did the following instructions
u<-cbind(0,0,0,0)
rt1<-rbind(t1[1:6,],u,t1[7:8,],u,t1[9,],u,u)
rt2<-rbind(t2[1:6,],u,t2[7:10,],u,u,t2[11,])
rt3<-rbind(t3[1:6,],u,t3[7:10,],u,u)
4) Now all my tables are 13 x 4 so I defined as matrix:
mrt1<-matrix(rt1,nrow=13,ncol=4)
mrt2<-matrix(rt2,nrow=13,ncol=4)
mrt3<-matrix(rt2,nrow=13,ncol=4)
5) And then I added them:
total<-mrt1+mrt2+mrt3
total
[,1] [,2] [,3] [,4]
[1,] 130 97 83 89
[2,] 53 55 42 46
[3,] 33 36 36 39
[4,] 5 1 2 3
[5,] 5 4 1 0
[6,] 1 1 1 3
[7,] 0 0 0 0
[8,] 8 6 4 4
[9,] 6 4 1 2
[10,] 1 2 2 3
[11,] 1 1 1 1
[12,] 0 0 0 0
[13,] 0 0 1 0
which is the result I wanted.
I don't know if this is the best way to make this kind of tables, but at
least it worked well on weekend late at night :P
If someone have a better (faster) way to make them please help me.
Warm regards from Mexico.
-----Mensaje original-----
De: Petr PIKAL [mailto:petr.pikal at precheza.cz]
Enviado el: Lunes, 03 de Diciembre de 2007 02:30 a.m.
Para: rodrigueza at schwabe.com.mx
CC: r-help at r-project.org
Asunto: Re: [R] Help with tables
Hi
I am not sure but if I remember correctly I had seen similar output in
Frank Harrel's book. So you could check some function from Hmisc package,
probably summarize.
Regards
Petr
petr.pikal at precheza.cz
r-help-bounces at r-project.org napsal dne 02.12.2007 03:16:09:
> I guess you can get the result by
> 1) concatenating all the variables (P2_A, P2_B, P2_C) into one variable
,
> 2) replicating segment membership properly,
> 3) make the table of 1) and 2)
>
> For example, the following may do the job.
>
> > ## (1) Generate data set
> > # Set random seed
> > set.seed(0)
> >
> > n.obs <- 15 # Number of patients
> > disease.lv <- 1:10 ## different types of disease
> > data1 <- as.data.frame(t(replicate(n.obs, sample(disease.lv, 3)))) #
> Create data set
> > ## Create segment membership
> > segment <- sample(LETTERS[1:3], n.obs, replace=TRUE)
> >
> > cbind(data1, segment)
> V1 V2 V3 segment
> 1 9 3 10 B
> 2 6 9 2 C
> 3 9 10 6 A
> 4 7 1 2 B
> 5 2 7 4 C
> 6 8 5 6 C
> 7 10 4 7 B
> 8 10 2 6 C
> 9 2 3 4 B
> 10 1 4 7 A
> 11 4 5 9 A
> 12 5 2 7 A
> 13 7 8 1 A
> 14 8 4 7 B
> 15 7 8 5 B
> >
> >
> > ## (2) Table
> > infec.all <- unlist(data1) # Concatenate all variables into one
variable
> > segment.all <- rep(segment, ncol(data1)) # Replicate the segment
> membership as necessary
> > table(infec.all, segment.all)
> segment.all
> infec.all A B C
> 1 2 1 0
> 2 1 2 3
> 3 0 2 0
> 4 2 3 1
> 5 2 1 1
> 6 1 0 3
> 7 3 4 1
> 8 1 2 1
> 9 2 1 1
> 10 1 2 1
>
>
>
> On Nov 30, 2007 10:10 AM, Alejandro Rodríguez
<rodrigueza at schwabe.com.mx>
> wrote:
>
> > Hello, I'm new using R and developing tables.
> >
> > I have a problem in developing a table. In a questionaire I made I
ask
> > this
> > question "Please tell me the first three sympthoms caused by
Respiratory
> > tract infection you've caught this year", then the people answer three
> > sympthoms, the first mention (Top of mind) is saved in a variable
called
> > "P2_A", the second mention in a variable called "P2_B" and the third
> > mention
> > in "P2_C". Each answer is coded with numbers like this:
> >
> > 1 = Flu
> > 2 = Cough
> > 3 = Asthma
> > ....
> >
> > 13 = Fever
> >
> > I've already done a K-cluster analysis and segmented my data base. So
my
> > first task is to develop tables to develop tables of frequencies
crossing
> > Cluster vs. "P2_A" in order to know which are the top of mind products
and
> > their frequencies by cluster, then the second mention and third
mention.
> > I've used this instruction which worked well:
> >
> > > table(infec1,aglomera)
> > aglomera
> > infec1 1 2 3 4
> > 1 117 88 76 83
> > 2 10 10 9 7
> > 3 15 11 14 14
> > 4 2 0 1 1
> > 5 2 3 1 0
> > 6 1 0 1 0
> > 8 3 3 0 1
> > 9 3 1 1 0
> > 11 0 0 1 1
> >
> > Where "infec1" is a factor of "P2_A" and "aglomera" is a factor of the
> > variable "Cluster" I made. It worked well when I study them
> > separately...however I would like to know the TOTAL mentions of each
> > sympthom by cluster. I've done this exercise in SPSS using the
"Multiple
> > Response" instruction first grouping my three variables (i.e. "P2_A",
> > "P2_B"
> > and "P2_C") into a variable called "sick" and cross tabulating it vs.
> > QCL_1
> > (my cluster variable) and it gave me the table I need in this way
(showed
> > at
> > the bottom of this mail):
> >
> > How can I made a table like this in R???. I've tried combining my
> > variables
> > in a matrix and using xtabs, ftable, table, structable and a lot of
> > combination of them but I haven't had succed with any of them.
> >
> > Please help me with this issue, I don't want to keep using SPSS any
more.
> >
> > Thanx in advance.
> >
> > P.D. Result from SPSS is shown below.
> >
> >
> >
> > * * * C R O S S T A B U L A T I O N * * *
> >
> > $SICK (group) mr sick
> > by QCL_1 Cluster Number of Case
> >
> >
> > QCL_1
> >
> > Count I
> > I Row
> > I Total
> > I 1 I 2 I 3 I 4 I
> > $SICK --------+--------+--------+--------+--------+
> > 1 I 130 I 97 I 83 I 89 I 399
> > Gripe, influenza, ca I I I I I 83.1
> > +--------+--------+--------+--------+
> > 2 I 53 I 55 I 42 I 46 I 196
> > Tos de cualquier tip I I I I I 40.8
> > +--------+--------+--------+--------+
> > 3 I 33 I 36 I 36 I 39 I 144
> > Dolor irritación I I I I I 30.0
> > +--------+--------+--------+--------+
> > 4 I 5 I 1 I 2 I 3 I 11
> > Bronquitis I I I I I 2.3
> > +--------+--------+--------+--------+
> > 5 I 5 I 4 I 1 I 0 I 10
> > Sinusitis I I I I I 2.1
> > +--------+--------+--------+--------+
> > 6 I 1 I 1 I 1 I 3 I 6
> > Rinitis I I I I I 1.3
> > +--------+--------+--------+--------+
> > 8 I 8 I 6 I 4 I 4 I 22
> > Amigdalitis I I I I I 4.6
> > +--------+--------+--------+--------+
> > 9 I 6 I 4 I 1 I 2 I 13
> > Faringitis I I I I I 2.7
> > +--------+--------+--------+--------+
> > 10 I 1 I 2 I 2 I 3 I 8
> > Laringitis I I I I I 1.7
> > +--------+--------+--------+--------+
> > 11 I 1 I 1 I 1 I 1 I 4
> > Neumonia I I I I I .8
> > +--------+--------+--------+--------+
> > 13 I 0 I 0 I 1 I 0 I 1
> > Asma I I I I I .2
> > +--------+--------+--------+--------+
> > Column 153 116 104 107 480
> > Total 31.9 24.2 21.7 22.3 100.0
> >
> > Percents and totals based on respondents
> >
> > 480 valid cases; 0 missing cases
> >
> >
> >
> > Act. Calef Alejandro Rodríguez Cuevas
> > Analista de mercado
> >
> > Laboratorios Farmasa S.A. de C.V.
> > Schwabe Mexico, S.A. de C.V.
> >
> > Bufalo Nr. 27
> > Col. del Valle 03100
> > Mexico, D.F.
> > Mexico
> >
> > Tel. 52 00 26 80
> > email: rodrigueza at schwabe.com.mx
> >
> > www.schwabe.com.mx
> > www.umckaloabo.com.mx
> >
> > ______________________________________________
> > 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.
> >
>
>
>
> --
> ======================================
> T.K. (Tae-kyun) Kim
> Ph.D. student
> Department of Marketing
> Marshall School of Business
> University of Southern California
> ======================================
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list