[R] Questions on counts by case

jim holtman jholtman at gmail.com
Wed Feb 7 00:23:39 CET 2007


On 2/6/07, Serguei Kaniovski <Serguei.Kaniovski at wifo.ac.at> wrote:
>
>
> Hi all,
>
>
> for the data below I would like to
>
> 1. generate a dummy variable for each group "gr" of the same composition by
> people, then save each portion in a separate file,
This gives back a list that you can go through and write out each
element to a different file:

> split(x, x$gr)
$gr1
  person  gr x
1   mike gr1 1
2   jane gr1 0
3   bill gr1 0

$gr2
  person  gr x
4   jack gr2 1
5   mike gr2 1
6   jane gr2 0
7   bill gr2 0
8   alex gr2 1
9  james gr2 1

$gr3
   person  gr x
10   mike gr3 0
11   bill gr3 1
12   jane gr3 1


>
> 2. compute the frequency of "1"'s in "x" for each person by group
> "gr". So, "mike" will have freq=2/3, as he has two "1" and one "0" in 3
> groups.
Is this what you want?

> x
   person  gr x
1    mike gr1 1
2    jane gr1 0
3    bill gr1 0
4    jack gr2 1
5    mike gr2 1
6    jane gr2 0
7    bill gr2 0
8    alex gr2 1
9   james gr2 1
10   mike gr3 0
11   bill gr3 1
12   jane gr3 1
> do.call(rbind, by(x, list(x$person), function(z)c("0"=sum(z$x == 0), "1"=sum(z$x == 1))))
      0 1
alex  0 1
bill  2 1
jack  0 1
james 0 1
jane  2 1
mike  1 2

>
>
> Thanks a lot,
>
> Serguei Kaniovski
>
>
> The data looks like:
>
> person;gr;x
>
> mike;gr1;1
>
> jane;gr1;0
>
> bill;gr1;0
>
> jack;gr2;1
>
> mike;gr2;1
>
> jane;gr2;0
>
> bill;gr2;0
>
> alex;gr2;1
>
> james;gr2;1
>
> mike;gr3;0
>
> bill;gr3;1
>
> jane;gr3;1
> ___________________________________________________________________
>
> AustrianInstituteofEconomicResearch(WIFO)
>
> Name:SergueiKaniovski       P.O.Box91
> Tel.:+43-1-7982601-231             ArsenalObjekt20
> Fax:+43-1-7989386                  1103Vienna,Austria
> Mail:Serguei.Kaniovski at wifo.ac.at  A-1030Wien
>
> http://www.wifo.ac.at/Serguei.Kaniovski
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at 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.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list