# [R] R: question about vectors + for loop

Guido Masarotto guido at hal.stat.unipd.it
Sat Jun 5 20:51:57 CEST 1999

```On Sat, Jun 05, 1999 at 04:14:11PM +0200, Maria Wolters wrote:
> a) is there a straightforward way of saying
>   for all factors f in a data frame {
>       any old function, e.g. max(f)
>   }

I am not sure to understand completly the question. Anyway, if
d is a data.frame, you can extract the factors in d using something
like
> d.only.factor <- d[,unlist(lapply(d,is.factor))]
Then, you can use lapply again to apply a 'old function' to the
columns of d.only.factor. However, keep in mind that max, sum and
like are considered not meaningful for factor. So, if you really
insist in computing the max of the d.only.factor columns
(assuming that this make sense) you should use something like
> lapply(d.only.factor, function(x) max(as.numeric(x)))

If this sounds cryptic, look to the manual pages (e.g., ?lapply,
?unlist,...) and perhaps to some of the  suggested reading
listed in the FAQ (the recommended one is
W. N. Venables and B. D. Ripley (1997),
"Modern Applied Statistics with S-PLUS. Second Edition".
Springer, ISBN 0-387-98214-0
Third edition is forthcoming (Brian Ripley can be more precise)
so my suggestion is to get a copy from a library, not from a book
shop)

> 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 :)

d.XequaltoA <- d[d\$X=="A",]
should do the work. Easy, isn't it?

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

I suspect that you must try to be more specific (and maybe,
fill a bug report (?bug.report))

Hoping this can help.

guido

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```