# Re: [R] A. Mani : Avoiding loops

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Fri 19 Aug 2005 - 17:58:00 EST

Hi

Or maybe table

> mat<-matrix(sample(LETTERS[1:4], 200, replace=T),40,5)
> df<-data.frame(mat)

> table(df\$X4)

A B C D
8 12 11 9
>

is what is wanted

HTH
Petr

On 19 Aug 2005 at 7:24, Sean O'Riordain wrote:

> Hi,
> I'm not sure what you actually want from your email (following the
> posting guide is a good way of helping you explain things to the rest
> of us in a way we understand - it might even answer your question!
>
> I'm only a beginner at R so no doubt one of our expert colleagues will
> help me...
>
> > fred <- data.frame()
> > fred <- edit(fred)
> > fred
> A B C D E
> 1 1 2 X Y 1
> 2 2 3 G L 1
> 3 3 1 G L 5
> > fred[,3]
> [1] X G G
> Levels: G X
> > fred[fred[,3]=="G",]
> A B C D E
> 2 2 3 G L 1
> 3 3 1 G L 5
>
> so at this point I can create a new dataframe with column 3 (C) ==
> "G"; either explicitly or implicitly...
>
> and if I want to calculate the sum() of column E, then I just say
> something like...
>
> > sum(fred[fred[,3]=="G",][,5])
> [1] 6
> >
>
> now naturally being a bit clueless at manipulating stuff in R, I
> didn't know how to do this before I started... and you guys only get
> to see the lines that I typed in and got a "successful" result...
>
> according to section 6 of the "Introduction to R" manual which comes
> with R, I could also have said > sum(fred[fred\$C=="G",]\$E) [1] 6
>
> Hmmm.... I wonder would it be reasonable to put an example of this
> type into section 2.7 of the "Introduction to R"?
>
>
> cheers!
> Sean
>
>
> On 18/08/05, A. Mani <a_mani_sc_gs@vsnl.net> wrote:
> > Hello,
> > I want to avoid loops in the following situation. There is a
> > 5-col
> > dataframe with col headers alone. two of the columns are
> > non-numeric. The problem is to calculate statistics(scores) for each
> > element of one column. The functions depend on matching in the other
> > non-numeric column.
> >
> > A B C E F
> > 1 2 X Y 1
> > 2 3 G L 1
> > 3 1 G L 5
> > and so on ...30000+ entries.
> >
> > I need scores for col E entries which depend on conditional
> > implications.
> >
> >
> > Thanks,
> >
> > A. Mani
> > Member, Cal. Math. Soc
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
> >
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help