Here is an example of the data.frame that I have,
df<-data.frame("case"=rep(1:5,each=9),"id"=rep(1:9,times=5),"x"=round(runif(length(rep(1:5,each=9)))))
"case" represents the cases,
"id" the persons, and
"x" is the binary state.
I would like to know in how many cases any two persons
a. both have "1",
b. the first has "0" - the second has "1",
c. the first has "0" - the second has "0",
d. both have "0".
There will be choose(9,2) sums, denoted s_ij for 1<=i<j<=9,
where i and j are running indices for an "id"-pair.
Please help, this is way beyond my knowledge of R!
Thank you,
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: Serguei.Kaniovski at wifo.ac.at
http://www.wifo.ac.at/Serguei.Kaniovski
library(gtools) apply(combinations(9,2), 1, function(x) with(df[df$id %in% x, ], table(x, id))) ------------------------------------------------------------------- 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 :> Here is an example of the data.frame that I have, > > df<-data.frame("case"=rep(1:5,each=9),"id"=rep(1:9,times=5),"x"=round(runif(length(rep(1:5,each=9))))) > > "case" represents the cases, > "id" the persons, and > "x" is the binary state. > > I would like to know in how many cases any two persons > > a. both have "1", > b. the first has "0" - the second has "1", > c. the first has "0" - the second has "0", > d. both have "0". > > There will be choose(9,2) sums, denoted s_ij for 1<=i<j<=9, > where i and j are running indices for an "id"-pair. > > Please help, this is way beyond my knowledge of R! > > Thank you, > Serguei Kaniovski
sorry, answered to quickly...
actually it's easier using paste():
df <- df[order(df$case),]
apply(combinations(9,2), 1, function(y) table(factor(do.call(paste,
c(with(df[df$id %in% y, ],
split(x, id)), sep="")),
levels=c("00","01","10","11"))))
-------------------------------------------------------------------
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 :> Here is an example of the data.frame that I have,
>
>
df<-data.frame("case"=rep(1:5,each=9),"id"=rep(1:9,times=5),"x"=round(runif(length(rep(1:5,each=9)))))
>
> "case" represents the cases,
> "id" the persons, and
> "x" is the binary state.
>
> I would like to know in how many cases any two persons
>
> a. both have "1",
> b. the first has "0" - the second has "1",
> c. the first has "0" - the second has "0",
> d. both have "0".
>
> There will be choose(9,2) sums, denoted s_ij for 1<=i<j<=9,
> where i and j are running indices for an "id"-pair.
>
> Please help, this is way beyond my knowledge of R!
>
> Thank you,
> Serguei Kaniovski