Hi,
lst1<- lapply(letters[1:3],function(i)
{df1<-data.frame(my_df[i],my_df["dat"]); res<-ddply(df1,.(df1[[i]]),function(x)
c("mean"=mean(x$dat),"n"=nrow(x)));names(res)[1]<-i;res<-res[res[,1]==1,]})
res1<-Reduce(function(...) merge(...,all=TRUE),lst1)
res1[is.na(res1)]<-"*"
res1
# mean n a
Thanks, John. Your solution gives me:
> ddply(my_df, .(a), summarize, mm = mean(dat), number = length(dat))
a mm number
1 0 14 3
2 1 11 3
I'm looking for (and Ista found a way):
>> a b c mean n
>> 1 1 * * 11 3
>> 2 * 1 * 14 3
>> 3 * * 1 12 3
thanks,
allie
On 3/20/2013 3:2
Nice, thanks Ista!
On 3/20/2013 3:18 PM, Ista Zahn wrote:
> How about
>
> library(reshape2)
> mdf.m <- melt(my_df, measure.vars=c("a", "b", "c"))
> mdf.m <- mdf.m[mdf.m$value > 0, ]
>
> ddply(mdf.m, "variable", function(x) c("mean"=mean(x$dat), "n"=nrow(x)))
>
> ?
>
> Best,
> Ista
>
> On Wed,
Will this do?
library(plyr)
ddply(my_df, .(a), summarize, mm = mean(dat), number = length(dat))
John Kane
Kingston ON Canada
> -Original Message-
> From: ashen...@ufl.edu
> Sent: Wed, 20 Mar 2013 14:57:36 -0500
> To: r-help@r-project.org
> Subject: [R] summarize dataframe based on
How about
library(reshape2)
mdf.m <- melt(my_df, measure.vars=c("a", "b", "c"))
mdf.m <- mdf.m[mdf.m$value > 0, ]
ddply(mdf.m, "variable", function(x) c("mean"=mean(x$dat), "n"=nrow(x)))
?
Best,
Ista
On Wed, Mar 20, 2013 at 3:57 PM, Alexander Shenkin wrote:
> Hi folks,
>
> I'm trying to figur
5 matches
Mail list logo