Dear Sarah,

Am Sonntag, den 31.07.2011, 18:10 -0400 schrieb Sarah Goslee:

> I would use something like this:
> 
> > x <- c(2,2,3,3,4,6)
> > table(x)
> x
> 2 3 4 6
> 2 2 1 1
> > x <- factor(x, levels=1:8)
> > table(x)
> x
> 1 2 3 4 5 6 7 8
> 0 2 2 1 0 1 0 0

awesome. Thank you.

Looking further I found the article »Thinking in R: vectors« on
R-bloggers [1]. The given example there returned a list though by using
`lapply()` and the author is asking at the end: »Any R experts out there
with suggestions for a non-lapply solution?«. So Derek-Jones, Sarah just
gave you the answer.

A further note regarding the example in [1], instead of

        length(X[X == n])

to count the number of occurrences you can also use `sum(X == n)`
relying on the fact that `TRUE` and `FALSE` are converted to the
integers `1` and `0`.


Thanks,

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