On Sun, 6 Jan 2002 ozric at web.de wrote:
> hello,
> i want use the crossTableFunction from "Kickstart Document"
Why? R has xtabs, and
> xtabs(Sepal.Length~Species,iris)
Species
setosa versicolor virginica
250.3 296.8 329.4
works, although it is to a dataset of frequencies so
xtabs(~Species,iris)
is a more typical usage.
> and use the iris-Data as example to prepare my dataSets !
> ....how i should transform the iris dataSet that this function works?
>
> thanks for advance,
> christian
>
>
> xtab<-function(formula,dataframe,dnn=NULL,fieldwidth=10,chisq=F) {
> + if(!missing(formula) && !missing(dataframe)) {
> + xt<-as.character(attr(terms(formula),"variables")[-1])
> + nxt<-length(xt)
> + if(nxt > 2) {
> + by.factor<-as.factor(dataframe[[xt[2]]])
> + factor.levels<-levels(by.factor)
> + nlevels<-length(factor.levels)
> + brkstats<-as.list(rep(0,nlevels))
> + names(brkstats)<-factor.levels
> + for(i in 1:nlevels) {
> + currentdata<-subset(dataframe,by.factor == factor.levels[i])
> + currentcount<-length(currentdata[[1]])
> + totalcount<-length(dataframe[[1]])
> + cat("\nCount
for",xt[1],"=",factor.levels[i],"is",
> +
currentcount,"(",round(100*currentcount/totalcount,1),"%)\n\n")
> +
next.formula<-as.formula(paste(paste("~"),paste(xt[2:nxt],collapse="+")))
> + xtab(next.formula,currentdata,dnn,fieldwidth,chisq)
> + }
> + }
> + else format.xtab(xt[1],xt[2],dataframe,dnn,fieldwidth,chisq)
> + }
> + else cat("Usage: xtab(formula[, dnn=NULL, fieldwidth = 10, chisq =
F])\n")
> + }
> > names(iris)
> [1] "Sepal.Length" "Sepal.Width"
"Petal.Length" "Petal.Width" "Species"
> > xtab(iris$Sepal.Length~iris$Species,iris)
> Error in sort(x) : only atomic vectors can be sorted
>
>
>
>
______________________________________________________________________________
> DSL-Stichtag ist der 31.12.2001 - Sie sparen jetzt noch 135 Euro!
> Highspeed-Surfen beim Testsieger + Exklusiv-Prämien: http://dsl.web.de
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._