[R] summarizing dataframe

Jim Lemon bitwrit at ozemail.com.au
Mon Jan 13 12:15:05 CET 2003


Alexander.Herr at csiro.au wrote:

Hi Listers,

> Surely, I just have a mental block and there is a more elegant way of
> creating a summary count (other than extracing it from ftable). I'd like
> to create a new data.frame containing counts of spell by loc ie have 
> three columns showing spell,loc,count. Below the data.frame...
>
> Any help appreciated
> Thanks Herry
>
>     spell loc
> 101   Parts   1
> 102 Overall   2
...

It's a bit hard to tell exactly what you want from the example. If the 
assumptions that "spell" is the name of the second column and "loc" is the 
name of the third are correct:

1) add a name for the first field
2) Use a single comma (or other separator) between your data fields

herr.df<-read.table("herr.dat",header=T,sep=",")

boggle<-as.data.frame.table(table(herr.df$spell,herr.df$loc))
Freq2<-as.numeric(boggle$Var2)*as.numeric(boggle$Freq)
aggregate(Freq2,by=list(Var1=boggle$Var1),FUN=sum)

Jim




More information about the R-help mailing list