Am Sonntag, den 31.07.2011, 15:19 -0700 schrieb Jeffrey Dick:
> Here's an attempt using sapply:
> 
> > x <- c(2, 2, 3, 3, 4, 6)
> > ys <- 1:8
> > sapply(ys, function(y) { length(which(x==y)) } )
> [1] 0 2 2 1 0 1 0 0

The last piece for my trials missing was `sapply()` which I overlooked
reading `?lapply()` inspired by [1].

So an alternative is

> x <- c(2, 2, 3, 3, 4, 6)
> ys <- 1:8
> sapply(ys, function(y) { sum(x==y) } )
[1] 0 2 2 1 0 1 0 0

which is of course overkill reading Dénes’ response.

> tabulate(x, 8)
[1] 0 2 2 1 0 1 0 0


Thank you all,

Paul


[1] http://www.r-bloggers.com/thinking-in-r-vectors/

Attachment: signature.asc
Description: This is a digitally signed message part

______________________________________________
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