[R] Re : Frequency count
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Wed Jan 5 00:17:44 CET 2005
ssim at lic.co.nz writes:
> Dear list,
>
> I have a dataset as follow and I would like to count the frequencies for
> the combination of the two variables (f1 and f2) for each id. I know it is
> should be straight forward, but I just don't know how to do it in R. Here
> is the SAS code I will use to get the output I want :
>
> proc means nway;
> class id f1 f2;
> var flag
> output out=temp;
>
>
> Dataset:
> id f1 f2 flag
> 798 1 2 1
> 777 0 2 1
> 798 2 2 1
> 777 0 2 1
> 777 1 1 1
>
> Output:
> Id=798
> 1-2 1
> 2-2 1
>
> Id=777
> 0-2 2
> 1-1 1
Here's one way:
> dd <- read.table(stdin(),header=TRUE)
0: id f1 f2 flag
1: 798 1 2 1
2: 777 0 2 1
3: 798 2 2 1
4: 777 0 2 1
5: 777 1 1 1
6:
> by(dd, dd$id, function(x)table(paste(x$f1,x$f2,sep="-")))
dd$id: 777
0-2 1-1
2 1
------------------------------------------------------------
dd$id: 798
1-2 2-2
1 1
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list