[R] R: question about vectors + for loop

Douglas Bates bates at stat.wisc.edu
Sat Jun 5 17:45:16 CEST 1999


Maria Wolters <wolters at ikp.uni-bonn.de> writes:

> first of all: I'm not only new to R, but also to S.

Welcome.

> I hope this is the right forum for asking the  following
> very stupid questions:

This is the right forum and no question is considered stupid here
unless you already know the answer.

> a) is there a straightforward way of saying
>   for all factors f in a data frame {
>       any old function, e.g. max(f)
>   }

It is a little obscure but you can use lapply for that.  A data.frame
can take on the behavior of a list or of a matrix, depending on the
context.  A simple way of doing what you want is
 lapply( mydataframe, max )
That will return the result as a list.  You may find a vector to be
more appealing, in which case you can use
 unlist( lapply( mydataframe, max ) )

This assumes that max will be meaningful for all the columns of your
data frame.  It is best to check that first using, for example,
 unlist( lapply( mydataframe, data.class ) )
The max function would not make sense for a factor, for example.

You could also look at the output for
 summary( mydataframe )
which produces some brief summary statistics for numeric columns.
These include a "five-number summary" with the min, max, quartiles,
and median.

> b) how can you construct a new data frame d' from 
>    a given data frame d which contains only rows
>    with [X=="A"] (x is a factor of d)?
>    I know there's an obvious way, but with 50+ columns,
>    that's a wee bit tedious :) 

The indexing in the S family of languages is very flexible.  You can
use logical expressions for a "column" index with the matrix-like view
of a data.frame.  The expression would look like

 d.new <- d[ d$X == "A", ]      # that comma is important

> c) could it be possible that there's a bug in the implementation of 
>     the Kolmogorov-Smirnov test? 

Bugs?  R could have bugs?  You must be joking!  :-)

The bug list for R is available at http://r-bugs.biostat.ku.dk/R

You could check that to see if anything has been reported.  If not,
you could use the bug.report() function to submit a bug report.

Hope this helps.

--Doug Bates
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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