[R] Help with tables
Petr PIKAL
petr.pikal at precheza.cz
Mon Dec 3 09:30:26 CET 2007
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