[R] Re : Frequency count
Tarmo Remmel
tarmo.remmel at utoronto.ca
Wed Jan 5 00:19:26 CET 2005
This might help...
Here KI is a matrix such as your dataset (without the flag)
"dups" <- function (intab=KI) {
# CREATE A MATRIX WITH ONLY THE UNIQUE ROWS OF intab
intabunique <- unique(intab)
# COLLAPSE THE FULL TABLE
a2 <- apply(intab, 1, paste, collapse=":")
# COLLAPSE THE UNIQUE TABLE
b2 <- apply(intabunique, 1, paste, collapse=":")
# COUNT UNIQUE ROW OCCURRENCES AND WRITE TO .COUNT
assign(".COUNT",c(table(c(a2,unique(b2)))[b2] - 1) ,pos=1)
# WRITE THE UNIQUE TABLE TO .KIUNIQUE
assign(".KIUNIQUE", intabunique, pos=1)
}
> KI
col1 col2 col3
[1,] 1 1 1
[2,] 2 1 1
[3,] 1 2 1
[4,] 1 1 1
[5,] 1 2 1
[6,] 1 1 1
[7,] 2 2 2
> dups(KI)
> .KIUNIQUE
col1 col2 col3
[1,] 1 1 1
[2,] 2 1 1
[3,] 1 2 1
[4,] 2 2 2
> .COUNT
1:1:1 2:1:1 1:2:1 2:2:2
3 1 2 1
______________________________________________
Tarmo Remmel B.E.S., M.Sc.F., Ph.D. Candidate
GUESS Lab, Department of Geography
University of Toronto, Toronto, ON, Canada
http://eratos.erin.utoronto.ca/remmelt
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of ssim at lic.co.nz
> Sent: Tuesday, January 04, 2005 5:36 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Re : Frequency count
>
>
> 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
> __________________________________________________________________
> _________
> This message, including attachments, is confidential. If you...{{dropped}}
>
> ______________________________________________
> 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
More information about the R-help
mailing list