[R] newbie: table,mean for all variables

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Jan 14 14:42:39 CET 2002


"C. Schulz" <ozric at web.de> writes:

> Hello,
> (1)
> what is the way to use 
> table for the hole dataSet ?
              /w\ 
 
> I would get a cross-table with x1 and other variables in the dataSet which have got 100 columns :
> 
>  table(x1,data[variable2 until variable 100])/length([variable2 until variable 100])*100     
> 
> like this but it's wrong ?
>  table(x1,data[,2:100])/length(data[,2:100])*100     
> 
> if i type: table(data[,1:10])   
> to get the frequency distribution of the first 10 variables ,
> i get not the wished tables ?

lapply is your friend:

lapply(Data[,2:100],function(i)prop.table(table(Data$x1,i),1))


> (2) 
> is it possible to set factors for
> all variables in a dataSet in one step 
> i.e. levels are all similar(?=NA,0,20,40,60,80,100)?

Data <- lapply(Data, factor, levels=...,labels=...)

> Additional how can i get all mean's in one step,too ?
> I know that i cannot use the mean-function, because
> i have NA's and factor variables.
> So have got anybody a good tip i.e.
> is it possible to get only the mean from the "summary" procedure ?

As of 1.4.0, mean(Data, na.rm=TRUE) should get it right. Otherwise
stuff like this:

> airquality$Month <- factor(airquality$Month)
> sapply(airquality[sapply(airquality,is.numeric)], mean, na.rm=T)
     Ozone    Solar.R       Wind       Temp        Day 
 42.129310 185.931507   9.957516  77.882353  15.803922 


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list