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}}
Try table(), aggregate(), or tapply(), using length() in the last two functions. I hope that this helps, Andrew On Wed, Jan 05, 2005 at 11:36:07AM +1300, ssim at lic.co.nz wrote:> 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-- Andrew Robinson Ph: 208 885 7115 Department of Forest Resources Fa: 208 885 6226 University of Idaho E : andrewr at uidaho.edu PO Box 441133 W : http://www.uidaho.edu/~andrewr Moscow ID 83843 Or: http://www.biometrics.uidaho.edu No statement above necessarily represents my employer's opinion.
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 1Here'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
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
On Wed, 5 Jan 2005 ssim at lic.co.nz wrote:> 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 :table() will count frequencies, and interaction() will create a variable with all combinations of a set of variables, so something like table(interaction(f1,f2)) -thomas