On Tue, Feb 17, 2009 at 10:00:40AM -0600, Marc Schwartz wrote:
> on 02/17/2009 09:06 AM Hans Ekbrand wrote:
> > Hi r-help!
> >
> > Consider the following data-frame:
> >
> >var1 var2 var3
> > 1 314
> > 2 223
> > 3 223
> > 4 44 NA
> > 5 435
> > 6 223
> > 7 343
> >
> > How can I get R to convert this into the following?
> >
> > Value 1 2 3 4 5
> > var1 0 3 2 2 0
> > var2 1 3 1 2 0
> > var3 0 0 4 1 1
>
>
> > t(sapply(DF, function(x) table(factor(x, levels = 1:5
> 1 2 3 4 5
> var1 0 3 2 2 0
> var2 1 3 1 2 0
> var3 0 0 4 1 1
>
>
> The key is to turn each column into a factor with explicitly defined
> common levels for tabulation. This enables the table result to have a
> consistent format across each column, allowing for a matrix to be
> created, rather than a list.
Thanks alot, Marc. Neat and efficient, just what I wanted.
BTW, before I saw that you actually included code, I tried on my own,
and wrote this:
my.count <- function(data.frame, levels) {
result.df <- data.frame(matrix(nrow=length(data.frame),ncol=levels))
for (i in 1:length(data.frame)) {
result.df[i,] <- table(factor(data.frame[[i]], levels = c(1:levels)))
}
result.df
}
which produces the same result. I take this to be a an instructive
example of unnecessary use of for-loops in R.
--
Hans Ekbrand (http://sociologi.cjb.net)
Q. What is that strange attachment in this mail?
A. My digital signature, see www.gnupg.org for info on how you could
use it to ensure that this mail is from me and has not been
altered on the way to you.
signature.asc
Description: Digital signature
__
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.