# A fake data
data<-data.frame(cbind(rep(c("Centre
1","Centre 2","Centre
3"),c(13,10,12)),rbinom(35,1,0.7)))
# Your result
data.frame(Proportion=tapply(data$X2,data$X1,function(x)
sum(x==1)/length(x)))
# Use names() and cbind() to create a two columns
data set.
From:
s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On
Behalf Of Hunsicker, Lawrence
Sent: Tuesday, 23 December 2008
9:06 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] How to create a
"summary" data frame
Hi,
folks, and Happy Holidays to all:
I
have a data frame with about 11,000 patients
from about 600 different centers. Roughly half of these patients
have had a certain test done, and the other half have not had
the test. But the fraction with the test varies from center to
center. I’d like to add a column to the data frame indicating the
fraction of patients at each center that had the test done. I tried
doing this using the GUI to calculate the average of 0 (no) and 1 (yes) values,
doing the average by center, and saving the result. I get a list with a
single value (the average) for each center, but the center IDs
are
not included, so that I can’t do a merge on center ID. How can I
create a data frame with two columns, the first column being the center number,
and the second being the fraction of patients with the test
done?
Larry
Hunsicker