[R] Compiling a contingency table of counts by case
I have asked a similar question before but this time the problem is somewhat more involved. I have the following data: case;name;x 1;Joe;1 1;Mike;1 1;Zoe;1 2;Joe;1 2;Mike;0 2;Zoe;1 2;John;1 3;Mike;1 3;Zoe;0 3;Karl;0 I would like to count the number of case in which any two name a. both have x=1, b. the first has x=0 - the second has x=1, c. the first has x=1 - the second has x=0, d. both have x=0, The difficulty is that the number of names and their identity changes from case to case. Thanks a lot for you help, Serguei Kaniovski -- ___ Austrian Institute of Economic Research (WIFO) Name: Serguei Kaniovski P.O.Box 91 Tel.: +43-1-7982601-231 Arsenal Objekt 20 Fax: +43-1-7989386 1103 Vienna, Austria Mail: [EMAIL PROTECTED] http://www.wifo.ac.at/Serguei.Kaniovski __ R-help@stat.math.ethz.ch 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.
Re: [R] Compiling a contingency table of counts by case
dat - read.delim(clipboard, sep=;) dat - dat[order(dat$case, dat$name), ] res - apply(combinations(nlevels(dat$name), 2), 1, function(x) with(dat[dat$name %in% levels(dat$name)[x],], table(unlist(sapply(split(x, case), function(y) ifelse(length(y) == 2, paste(y, collapse=), NA)) names(res) - apply(combinations(nlevels(dat$name), 2), 1, function(x) paste(levels(dat$name)[x], collapse=.)) res $Joe.John 11 1 $Joe.Karl character(0) $Joe.Mike 10 11 1 1 $Joe.Zoe 11 2 $John.Karl character(0) $John.Mike 10 1 $John.Zoe 11 1 $Karl.Mike 01 1 $Karl.Zoe 00 1 $Mike.Zoe 01 10 11 1 1 1 --- Jacques VESLOT CNRS UMR 8090 I.B.L (2ème étage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr --- Serguei Kaniovski a écrit : I have asked a similar question before but this time the problem is somewhat more involved. I have the following data: case;name;x 1;Joe;1 1;Mike;1 1;Zoe;1 2;Joe;1 2;Mike;0 2;Zoe;1 2;John;1 3;Mike;1 3;Zoe;0 3;Karl;0 I would like to count the number of case in which any two name a. both have x=1, b. the first has x=0 - the second has x=1, c. the first has x=1 - the second has x=0, d. both have x=0, The difficulty is that the number of names and their identity changes from case to case. Thanks a lot for you help, Serguei Kaniovski __ R-help@stat.math.ethz.ch 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.
Re: [R] Compiling a contingency table of counts by case
Thanks Jacques, this works! Serguei -- ___ Austrian Institute of Economic Research (WIFO) Name: Serguei Kaniovski P.O.Box 91 Tel.: +43-1-7982601-231 Arsenal Objekt 20 Fax: +43-1-7989386 1103 Vienna, Austria Mail: [EMAIL PROTECTED] http://www.wifo.ac.at/Serguei.Kaniovski __ R-help@stat.math.ethz.ch 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.
Re: [R] Compiling a contingency table of counts by case
I have asked a similar question before but this time the problem is somewhat more involved. I have the following data: case;name;x 1;Joe;1 1;Mike;1 1;Zoe;1 2;Joe;1 2;Mike;0 2;Zoe;1 2;John;1 3;Mike;1 3;Zoe;0 3;Karl;0 I would like to count the number of case in which any two name a. both have x=1, b. the first has x=0 - the second has x=1, c. the first has x=1 - the second has x=0, d. both have x=0, One way is to use the reshape package: dat - read.delim(clipboard, sep=;) dat - rename(dat, c(x=value)) cast(dat, name ~ case) (which doesn't give you exactly what you want, but I think might be more illuminating) Hadley __ R-help@stat.math.ethz.ch 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.
Re: [R] Compiling a contingency table of counts by case
what's different from: with(dat, tapply(x, list(name,case), sum)) 1 2 3 Joe 1 1 NA John NA 1 NA Karl NA NA 0 Mike 1 0 1 and how to deal with this table ? --- Jacques VESLOT CNRS UMR 8090 I.B.L (2ème étage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr --- hadley wickham a écrit : I have asked a similar question before but this time the problem is somewhat more involved. I have the following data: case;name;x 1;Joe;1 1;Mike;1 1;Zoe;1 2;Joe;1 2;Mike;0 2;Zoe;1 2;John;1 3;Mike;1 3;Zoe;0 3;Karl;0 I would like to count the number of case in which any two name a. both have x=1, b. the first has x=0 - the second has x=1, c. the first has x=1 - the second has x=0, d. both have x=0, One way is to use the reshape package: dat - read.delim(clipboard, sep=;) dat - rename(dat, c(x=value)) cast(dat, name ~ case) (which doesn't give you exactly what you want, but I think might be more illuminating) Hadley __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Compiling a contingency table of counts by case
what's different from: with(dat, tapply(x, list(name,case), sum)) 1 2 3 Joe 1 1 NA John NA 1 NA Karl NA NA 0 Mike 1 0 1 and how to deal with this table ? Well, the syntax is easier (once you have the data in the correct, molten, form), and more flexible for other tasks. It is surely better to learn a general purpose tool rather than a tool for a specific task. To use that table to answer the original question, you just need to look column by column for the desired patterns of 0's and 1's, eg. in case 1, Joe, Mike and Zoe all had ones. Perhaps I misunderstood the original question. Hadley __ R-help@stat.math.ethz.ch 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.