HI Farnoosh,

May be this helps:

set.seed(497)
dat <- data.frame(Unit=sample(10, 200,replace=TRUE), MD= 
sample(5,200,replace=TRUE), Response=sample(3, 200, replace=TRUE))
library(reshape2)
res <- dcast(dat, Unit+MD~Response, value.var="Response", length)
 res[,-(1:2)] <- round((res[,-(1:2)]/rowSums(res[,-(1:2)]))*100,2)
res1 <- res
res1[,-(1:2)] <- lapply(res1[-(1:2)],paste0,"%")
 head(res1)
#  Unit MD      1      2      3
#1    1  1     0% 33.33% 66.67%
#2    1  2 33.33% 33.33% 33.33%
#3    1  3     0%    50%    50%
#4    1  4    40%    40%    20%
#5    1  5     0%   100%     0%
#6    2  1    60%    20%    20%
A.K.



I think I want to have something like this:


Unit, MD, Response 1, Response 2, Response 3
11         90%8%2%
1280%10%                 10%
2395%3%2%

Does this make sense to you?


Regards, Farnoosh Sheikhi


On Tuesday, May 27, 2014 7:49 PM, arun <smartpink...@yahoo.com> wrote:



Hi Farnoosh,

I am not sure whether this is what you wanted.
library(plyr)

freq1 <- acast(data,MD~Unit, value.var="Response",length)
 round((freq1/colSums(freq1))*100,2)

Regarding the barplots, there would be some options.
?barplot
library(lattice)
?barchart
If you have any particular format (e.g. online reference) in mind, let me know.

Tx.
Arun







On Tuesday, May 27, 2014 7:43 PM, farnoosh sheikhi <farnoosh...@yahoo.com> 
wrote:



Hi Arun,

I hope you are doing well.
I have a data set similar to below:

Response = rep((1:3), 560)
MD<-rep(1:112, each=15)
Unit<-rep(1:12, 140)
data<-as.data.frame(cbind(Unit, MD,Response ))


I want to get frequencies and proportion of Response for each MD within Unit. I 
also wanted to plot (bar plot) Response frequencies within Units in one plot.

Any help would be appreciated.
Thanks tons.

Regards, Farnoosh Sheikhi

______________________________________________
R-help@r-project.org 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.

Reply via email to