[R] avoiding a loop?

tom sgouros tomfool at as220.org
Mon Oct 15 21:12:41 CEST 2007


I knew it was simple.  Thanks very much.

 -tom


jim holtman <jholtman at gmail.com> wrote:

> ?table   to count the factors
> 
> > x
> [1] "a" "b" "c" "d" "e"
> > paste(head(x, -1), tail(x, -1), sep='')
> [1] "ab" "bc" "cd" "de"
> 
> 
> On 10/15/07, Tom Sgouros <tomfool at as220.org> wrote:
> >
> > Hi All:
> >
> > I feel like there must be a slick R-native no-loop way to get the counts
> > for the entries in a factor, but I'm unable to see how.  Right now I'm
> > doing this:
> >
> > hn.ent<-data.frame(rep(0,length(levels(hnf))), row.names=levels(hnf))
> > colnames(hn.ent)<-c("count")
> > for (lev in levels(hnf)) {
> >  hn.ent[lev,"count"] <- sum(lev == hnf)
> > }
> >
> > I'm sure there's a better way, but I don't know it.  Can anyone help?
> >
> > Also, why does hn.ent[lev,"count"] work and hn.ent$count[lev] does not?
> >
> > In a related problem, I have a vector of strings, and want a vector of
> > the strings combined.  That is, I have c("a", "b", "c", "d") and I want
> > c("ab", "bc", "cd").  I see this is easy with numbers, but paste() is
> > the only way I see to stick strings together, and I don't get how I'd
> > use them without a loop.
> >
> > Many thanks,
> >
> >  -tom
> >
> > --
> >  ------------------------
> >  tomfool at as220 dot org
> >  http://sgouros.com
> >  http://whatcheer.net
> >
> > ______________________________________________
> > R-help at 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.
> >
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem you are trying to solve?
> 


-- 
 ------------------------
 tomfool at as220 dot org
 http://sgouros.com  
 http://whatcheer.net



More information about the R-help mailing list