Hi, first of all: I'm not only new to R, but also to S. I hope this is the right forum for asking the following very stupid questions: a) is there a straightforward way of saying for all factors f in a data frame { any old function, e.g. max(f) } 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 :) c) could it be possible that there's a bug in the implementation of the Kolmogorov-Smirnov test? Thanks in advance for your help, Maria Wolters wolters at ikp.uni-bonn.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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._