[R] Suppressing final spaces in data.frame printouts

2009-11-11 Thread Stavros Macrakis
When printing data.frames, R aligns columns by padding with spaces. For example, print(data.frame(x=c('a','bb','ccc')),right=FALSE)   x 1 a  |-- vertical bar shows end of line 2 bb |-- vertical bar shows end of line 3 ccc|-- vertical bar shows end

Re: [R] Suppressing final spaces in data.frame printouts

2009-11-11 Thread Stavros Macrakis
M. Heiberger r...@temple.eduwrote: Stavros Macrakis wrote: I could of course write my own print function for this, but was wondering if there was a standard way of doing it. If not in R, perhaps there is some way to have ESS delete the final spaces? ESS, or more precisely emacs, can

Re: [R] Suppressing final spaces in data.frame printouts

2009-11-11 Thread Stavros Macrakis
complication which made me hope that someone had already solved the problem in R or ESS -s On Wed, Nov 11, 2009 at 8:43 PM, RICHARD M. HEIBERGER r...@temple.eduwrote: On Wed, Nov 11, 2009 at 8:12 PM, Stavros Macrakis macra...@alum.mit.edu wrote: Thanks for the suggestion

[R] Method dispatch for function

2009-11-18 Thread Stavros Macrakis
How can I determine what S3 method will be called for a particular first-argument class? I was imagining something like functionDispatch('str','numeric') = utils:::str.default , but I can't find anything like this. For that matter, I was wondering if anyone had written a version of `methods`

Re: [R] Problem with expand.grid

2009-12-22 Thread Stavros Macrakis
Unfortunately, expand.grid doesn't validate the class of its argument, so it is reporting an internal error rather than something more intelligible. On Tue, Dec 22, 2009 at 11:19 AM, Keith Jewell k.jew...@campden.co.ukwrote: Just confirming it isn't the bug fixed in 2.11.0dev, and giving an

Re: [R] solving cubic/quartic equations non-iteratively

2010-01-05 Thread Stavros Macrakis
There are certainly formulas for solving polynomials numerically up to 4th degree non-iteratively, but you will almost certainly get better results using iterative methods. Even the much more trivial formula for the 2nd degree (quadratic) is tricky to implement correctly and accurately, see: *

Re: [R] solving cubic/quartic equations non-iteratively

2010-01-22 Thread Stavros Macrakis
On Tue, Jan 5, 2010 at 5:25 PM, Carl Witthoft c...@witthoft.com wrote: quote: There are certainly formulas for solving polynomials numerically up to 4th degree non-iteratively, but you will almost certainly get better results using iterative methods. I must be missing something here.

Re: [R] first value...

2009-06-23 Thread Stavros Macrakis
I think what you mean is that you want to find the position of the first non-NA value in the vector. is.na returns a boolean vector of the NA values, so: xx - c(NA,NA,NA,2,3,NA,4) which(!is.na(xx))[1] [1] 4 The other proposed solution, which(diff(is.na(inc)) 0) is incorrect:

Re: [R] How to avoid ifelse statement converting factor to character

2009-06-24 Thread Stavros Macrakis
On Wed, Jun 24, 2009 at 12:34 PM, Mark Namtb...@gmail.com wrote: The problem is that after running the ifelse statement, data$SOCIAL_STATUS is converted from a factor to a character. Is there some way I can avoid this conversion? I'm afraid that ifelse has very bizarre semantics when the yes

Re: [R] How to avoid ifelse statement converting factor to character

2009-06-25 Thread Stavros Macrakis
On Wed, Jun 24, 2009 at 9:04 PM, Rolf Turnerr.tur...@auckland.ac.nz wrote:  Do not get your knickers in a twist.  R works simply and straightforwardly  in simple straightforward situations. Though I find R an incredibly useful tool, alas, it is simply not true that R works simply and

Re: [R] How to avoid ifelse statement converting factor to character

2009-06-25 Thread Stavros Macrakis
Erratum:     ifelse(TRUE,dd,dd) = 1230786000 (class numeric) should be ifelse(TRUE,tt,tt) = 1230786000 (class numeric) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide

Re: [R] How to avoid ifelse statement converting factor to character

2009-06-25 Thread Stavros Macrakis
On Thu, Jun 25, 2009 at 12:47 PM, Craig P. Pyramecrap...@gmail.com wrote: The man page Stavros quotes states that the class attribute of the result is taken from 'test', which clearly is not the case: Actually, the behavior is documented pretty clearly: The mode of the answer will be

Re: [R] How to avoid ifelse statement converting factor to character

2009-06-26 Thread Stavros Macrakis
It gives me a headache, too! I think you'll have to wait for a more expert user than me to supply explanations of these behaviors and their rationales. -s On 6/26/09, Craig P. Pyrame crap...@gmail.com wrote: Stavros Macrakis wrote: On Thu, Jun 25, 2009 at 12:47 PM, Craig P

Re: [R] How do I get just the two last tokens of each string in a vector?

2009-06-26 Thread Stavros Macrakis
One way is: a - c( %L H*L L*H H%, %L H* H%, %L L*H %, %L L*H % ) sub(^.*(^| )([^ ]+ [^ ]+$),\\2,a) [1] L*H H% H* H% L*H % L*H % Just be aware that this is not terribly efficient for very large strings. -s On Fri, Jun 26, 2009 at 7:21 AM, Fredrik Karlssondargo...@gmail.com

Re: [R] Automatically placing a legend in an area with the most white space...

2009-06-28 Thread Stavros Macrakis
install.packages('plotrix') On Sun, Jun 28, 2009 at 3:51 PM, Jason Rupert jasonkrup...@yahoo.comwrote: ... Error in legend(emptyspace(rep(x_vals_1, 3), c(y1_vals, y2_vals, y3_vals)), : could not find function emptyspace I've searched via RSeek, but I have not been able to find anything

Re: [R] Numbering sequences of non-NAs in a vector

2009-07-07 Thread Stavros Macrakis
Here's one possibility: vv - c(10,8,1,3,0,8,NA,NA,NA,NA,2,1,6,NA,NA,NA,0,5,1,9) (1+cumsum(diff(is.na(c(vv[1],vv)))==1)) * !is.na(vv) [1] 1 1 1 1 1 1 0 0 0 0 2 2 2 0 0 0 3 3 3 3 On Tue, Jul 7, 2009 at 5:08 PM, Krishna Tateneni taten...@gmail.com wrote: Greetings, I have a vector of the

Re: [R] Question in using e1071 svm routine

2009-07-07 Thread Stavros Macrakis
Isn't the initial value of the variable T equal to the constant TRUE? So unless he's modified the value of T, shouldn't it work? -s On 7/7/09, Max Kuhn mxk...@gmail.com wrote: Unlike Splus, R does not use T for TRUE. On Tue, Jul 7, 2009 at 6:05 PM, Michaelcomtech@gmail.com

Re: [R] strange strsplit gsub problem 0 is this a bug or a string length limitation?

2009-07-10 Thread Stavros Macrakis
On Fri, Jul 10, 2009 at 8:58 AM, Marc Schwartz marc_schwa...@me.com wrote: Review the Note in ?as.character: as.character truncates components of language objects to 500 characters (was about 70 before 1.3.1). If this limitation is too hard to fix, shouldn't it at least give a warning or an

Re: [R] Trig functions strange results

2009-07-14 Thread Stavros Macrakis
On Tue, Jul 14, 2009 at 1:45 PM, Nair, Murlidharan T mn...@iusb.edu wrote: I am trying to calculate coordinate transformations and in the process of debugging my code using debug I found the following Browse[1] direction[i] [1] -1.570796 Browse[1] cos(direction[i]) [1] 6.123032e-17

Re: [R] dereferencing in R

2009-07-16 Thread Stavros Macrakis
What do you mean by 'passing an array reference' and 'dereferencing' and what do you mean by an 'R script'? What language(s) are you accustomed to? If you mean 'passing an array value' to an 'R function', you just use the argument name. Since R uses call-by-value (modulo the substitute

Re: [R] Simple cat statement - output truncated

2009-07-16 Thread Stavros Macrakis
Kevin, The habitués of this mailing list get irritated when users mail in problem reports which don't include enough information to reproduce the problem, as requested in the standard footer of r-help mail (PLEASE ... provide commented, minimal, self-contained, reproducible code.) This irritation

Re: [R] quoting expressions in a list

2009-07-16 Thread Stavros Macrakis
On Thu, Jul 16, 2009 at 4:44 PM, Erik Iversoneiver...@nmdp.org wrote: I have a list of logical expressions, and I would really like it if the names of the components of the list were identical to the corresponding logical expression. So, as an example: df.example - data.frame(a = 1:10, b

[R] Object equality for S4 objects

2009-07-29 Thread Stavros Macrakis
To test two environments for object equality (Lisp EQ), I can use 'identity': e1 - environment(local(function()x)) e2 - environment(local(function()x)) identical(e1,e2) # compares object identity [1] FALSE identical(as.list(e1),as.list(e2))# compares values as

Re: [R] Object equality for S4 objects

2009-07-30 Thread Stavros Macrakis
On Thu, Jul 30, 2009 at 12:01 PM, Martin Morganmtmor...@fhcrc.org wrote: S4 objects do not have the semantics of environments, but of lists (or of most other R objects), so it is as meaningful to ask why identical(s1, s2) returns TRUE as it is to ask why identical(list(x=1), list(x=1))

Re: [R] Object equality for S4 objects

2009-07-30 Thread Stavros Macrakis
On Thu, Jul 30, 2009 at 4:03 PM, Martin Morganmtmor...@fhcrc.org wrote: S4 objects are mutable in the sense that one can write replacement methods for them Understood, but I don't think that's the usual meaning of 'mutable'. -s __

[R] Inconsistency in representation of variables

2009-05-11 Thread Stavros Macrakis
In stats::D, I was wondering why variables are represented as symbols in expressions, but as strings in lists of variables: D(quote(x^2),x) = 2*x D(quote(x^2),quote(x)) = error Variable must be a character string Strings are not allowed in the expression to denote variables:

Re: [R] Beyond double-precision?

2009-05-11 Thread Stavros Macrakis
On Sat, May 9, 2009 at 12:17 PM, Berwin A Turlach ber...@maths.uwa.edu.au wrote: log(H) = log(n) - log( 1/x_1 + 1/x_2 + ... + 1/x_n) ...But we need to calculate the logarithm of a sum from the logarithms of the individual terms. ...The way to calculate log(x+y) from lx=log(x) and ly=log(y) ...

Re: [R] can you tell what .Random.seed *was*?

2009-05-15 Thread Stavros Macrakis
On Thu, May 14, 2009 at 3:36 PM, G. Jay Kerns gke...@ysu.edu wrote: set.seed(something) x - rnorm(100) y - runif(500) # bunch of other stuff ... Now, I give you a copy of my script.R (with the set.seed statement removed, of course) together with the .RData file that was generated by the

Re: [R] can you tell what .Random.seed *was*?

2009-05-15 Thread Stavros Macrakis
On Fri, May 15, 2009 at 12:07 PM, Stavros Macrakis macra...@alum.mit.edu wrote: system.time(whatseed(runif(1))) Sorry, though I got lucky and my overall result is roughly correct, this is an incorrect time measure. It should be r - runif(1); system.time(whatseed(r)) because R's call

[R] Converting numbers to and from raw

2009-05-15 Thread Stavros Macrakis
How can I convert an integer or double to and from their internal representation as raws of length 4 and 8? The following works for positive integers (including those represented as floats): # Convert integer (represented as integer or double) to sequence # of raw bytes, least-significant byte

Re: [R] newbie: closing unused connection + readline

2009-05-16 Thread Stavros Macrakis
On Sat, May 16, 2009 at 8:34 AM, Aval Sarri aval.sa...@gmail.com wrote: # Create a socket from which to read lines - one at a time (record) reader.socket -   socketConnection( host = 'localhost', 5000,                                     server = TRUE, blocking = TRUE,                          

Re: [R] newbie: closing unused connection + readline

2009-05-16 Thread Stavros Macrakis
On Sat, May 16, 2009 at 9:11 AM, Aval Sarri aval.sa...@gmail.com wrote: ...I tried something line this also: mydataframe - read.table (socket, sep=,); but does not work says no input lines. this also. mydataframe - read.table (readLine(socket), sep=,); Sorry, I didn't see this before my

Re: [R] Gamma function

2009-05-16 Thread Stavros Macrakis
What exactly is the R code you wrote for your function f? Without that, it will be hard to help you. -s On Sat, May 16, 2009 at 2:48 AM, Kon Knafelman konk2...@hotmail.com wrote: Hi Guy, I am having trouble graphing the following function √2Γ(n/2)/[√n - 1Γ((n - 1)/2 for the

Re: [R] Concatenating two vectors into one

2009-05-18 Thread Stavros Macrakis
If you want to concatenate the *vectors*, you need 'c', which will also coerce the elements to a common type. If you want to concatenate the corresponding *elements* of the vectors, you need 'paste', which will coerce them to character strings. -s On 5/18/09, Henning Wildhagen

[R] Generic 'diff'

2009-05-18 Thread Stavros Macrakis
I would like to apply a function 'f' to the lagged version of a vector and the vector itself. This is easy to do explicitly: mapply( f, v[-1], v[-length(v)] ) or in the case of a pointwise vector function, simply f( v[-1], v[-length(v)] ) This is essentially the same as 'diff' but

Re: [R] Generic 'diff'

2009-05-18 Thread Stavros Macrakis
for that. For some examples see: methods(diff) On Mon, May 18, 2009 at 4:24 PM, Stavros Macrakis macra...@alum.mit.edu wrote: I would like to apply a function 'f' to the lagged version of a vector and the vector itself. This is easy to do explicitly: mapply( f, v[-1], v[-length(v

Re: [R] Generic 'diff'

2009-05-18 Thread Stavros Macrakis
not understanding your objection. -s On Mon, May 18, 2009 at 5:48 PM, Stavros Macrakis macra...@alum.mit.edu wrote: I guess I wasn't very clear. The goal is not to define diff on a different object type, but to have a different 'subtraction' operator with the same lag logic. An easy

Re: [R] exists function on list objects gives always a FALSE

2009-05-19 Thread Stavros Macrakis
On Tue, May 19, 2009 at 12:07 PM, Žroutík zrou...@gmail.com wrote: SmoothData - list(exists=TRUE, span=0.001) exists(SmoothData$span) FALSE As others have said, this just checks for the existence of a variable with the (strange) name SmoothData$span. In some sense, in R semantics, xxx$yyy

Re: [R] Functions returning functions

2009-05-20 Thread Stavros Macrakis
On Wed, May 20, 2009 at 7:21 AM, Paulo Grahl pgr...@gmail.com wrote: A - function(parameters) { # calculations w/ parameters returning 'y' tmpf - function(x) { # function of 'y' } return(tmpf) } The value of the parameters are stored in an environment local to the function.

Re: [R] Too large a data set to be handled by R?

2009-05-20 Thread Stavros Macrakis
On Tue, May 19, 2009 at 11:59 PM, tsunhin wong thjw...@gmail.com wrote: In order to save time, I am planning to generate a data set of size 1500 x 2 with each data point a 9-digit decimal number, in order to save my time. I know R is limited to 2^31-1 and that my data set is not going to

[R] Class for time of day?

2009-05-20 Thread Stavros Macrakis
What is the recommended class for time of day (independent of calendar date)? And what is the recommended way to get the time of day from a POSIXct object? (Not a string representation, but a computable representation.) I have looked in the man page for DateTimeClasses, in the Time Series

Re: [R] Class for time of day?

2009-05-21 Thread Stavros Macrakis
On Wed, May 20, 2009 at 12:28 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: There is a times class in the chron package. Perfect! Just what I was looking for. On Wed, May 20, 2009 at 12:19 PM, jim holtman jholt...@gmail.com wrote: If you want the hours from a POSIXct, here is one

Re: [R] Class for time of day?

2009-05-22 Thread Stavros Macrakis
On Thu, May 21, 2009 at 8:28 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: It uses hours/minutes/seconds for values 1 day and uses days and fractions of a day otherwise. Yes, my examples were documenting this idiosyncracy. For values and operations that it has not considered it

Re: [R] Class for time of day?

2009-05-22 Thread Stavros Macrakis
On Fri, May 22, 2009 at 10:03 AM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Regarding division you could contribute that to the chron package. I've contributed a few missing items and they were incorporated. Good to know. Maybe I'll do that Giving an error when it does not

Re: [R] Class for time of day?

2009-05-22 Thread Stavros Macrakis
On Fri, May 22, 2009 at 12:28 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: ...The way this might appear in code is if someone wanted to calculate the number of one hour intervals in 18 hours. One could write: t18 - times(18:00:00) t1 - times(1:00:00) as.numeric(t18) /

Re: [R] XML parse error

2009-05-24 Thread Stavros Macrakis
On Sun, May 24, 2009 at 12:28 PM, kulwinder banipal kbani...@hotmail.comwrote: It is for sure little complicated then a plain XML file. The format of binary file is according to XML schema. I have been able to get C parser going to get information from binary with one caveat - I have to

[R] OWL (Web Ontology Language) in R?

2009-05-26 Thread Stavros Macrakis
Is anyone working on an R package for manipulating OWL (Web Ontology Language), either natively or via an external library? I don't see anything obviously relevant in CRAN, though of course OWL functionality could be built up starting with the XML package. Thanks, -s

Re: [R] How to exclude a column by name?

2009-05-27 Thread Stavros Macrakis
On Wed, May 27, 2009 at 6:37 AM, Zeljko Vrba zv...@ifi.uio.no wrote: Given an arbitrary data frame, it is easy to exclude a column given its index: df[,-2]. How to do the same thing given the column name? A naive attempt df[,-name] did not work :) Various ways: Boolean index vector:

Re: [R] Defining functions - an interesting problem

2009-05-27 Thread Stavros Macrakis
The 'ties.method' argument to 'rank' is the *third* positional argument to 'rank', so either you need to put it in the third position or you need to use a named argument. The fact that the variable you're using to represent ties.method is called ties.method is irrelevant. That is, this:

[R] R Books listing on R-Project

2009-05-27 Thread Stavros Macrakis
I was wondering what the criteria were for including books on the Books Related to R page http://www.r-project.org/doc/bib/R-books.html. (There is no maintainer listed on this page.) In particular, I was wondering why the following two books are not listed: * Andrew Gelman, Jennifer Hill, *Data

Re: [R] custom sort?

2009-05-28 Thread Stavros Macrakis
I agree that it is surprising that R doesn't provide a sort function with a comparison function as argument. Perhaps that is partly because calling out to a function for each comparison is relatively expensive; R prefers vector operations. That said, many useful custom sorts are easy to define by

[R] RODBC package: how to check whether connection is open

2009-05-28 Thread Stavros Macrakis
What is the recommended way of checking whether an RODBC connection is open? Since odbcValidChannel is not exported from namespace RODBC, I suppose I shouldn't be using it. This is the best I could come up with, but it seems a bit 'dirty' to be using a tryCatch for something like this:

Re: [R] custom sort?

2009-05-28 Thread Stavros Macrakis
I couldn't get your suggested method to work: `==.foo` - function(a,b) unclass(a)==unclass(b) `.foo` - function(a,b) unclass(a) unclass(b) # invert comparison is.na.foo - function(a)is.na(unclass(a)) sort(structure(sample(5),class=foo)) #- 1:5 -- not reversed What am I missing?

[R] max.col specification

2009-05-29 Thread Stavros Macrakis
I'm not sure I understand the max.col spec or its rationale. In particular: * What is the significance and effect of assuming that the entries are probabilities, as they do not seem to be limited to the interval [0,1]? * In what contexts is it useful for max.col to consider numbers within a

Re: [R] maxtrix to permutation vector

2009-05-29 Thread Stavros Macrakis
Not sure what you mean by permutations here. I think what you mean is that given a matrix m, you want a matrix whose rows are c(i,j,m[i,j]) for all i and j. You can use the `melt` function in the `reshape` package for this. See below. Hope this helps, -s library(reshape)

Re: [R] maxtrix to permutation vector

2009-05-29 Thread Stavros Macrakis
value 1 a x 1 2 b x 2 3 a y 3 4 b y 4 as.matrix(melt(m)) x y value [1,] a x 1 [2,] b x 2 [3,] a y 3 [4,] b y 4 On Fri, May 29, 2009 at 2:53 PM, Stavros Macrakis macra...@alum.mit.eduwrote: Not sure what you mean by permutations here. I think what you mean

Re: [R] Error:non-numeric argument in my function

2009-05-31 Thread Stavros Macrakis
On Sun, May 31, 2009 at 6:10 PM, jim holtman jholt...@gmail.com wrote: Message is very clear: 1 * 'a' Error in 1 * a : non-numeric argument to binary operator Though the user should have been able to figure this out, perhaps the error message could be improved? After all, it is not the

Re: [R] Error:non-numeric argument in my function

2009-06-01 Thread Stavros Macrakis
. If the message said that a list of class 'data.frame' was found (probably the leading case), then that would be much more helpful. Patrick Burns patr...@burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of The R Inferno and A Guide for the Unwilling S User) Stavros

[R] all.equal(0,0i)

2009-06-02 Thread Stavros Macrakis
all.equal(0,0i) [1] Modes: numeric, complex [2] target is numeric, current is complex all.equal(1,1+0i) [1] Modes: numeric, complex [2] target is numeric, current is complex Is this the intended behavior? In general, all.equal is strict about argument mode, thus TRUE/1 and 1/'1' do not

Re: [R] Done: Fast way of finding top-n values of a long vector

2009-06-05 Thread Stavros Macrakis
On Fri, Jun 5, 2009 at 4:09 AM, Allan Engelhardt all...@cybaea.com wrote: I'm all done now. The max2 version below is what I went with in the end for my proposed change to caret::nearZeroVar (which used the sort method). Max Kuhn will make it available on CRAN soon. It speeds up that routine

Re: [R] if else

2009-06-08 Thread Stavros Macrakis
On Mon, Jun 8, 2009 at 1:48 PM, Cecilia Carmo cecilia.ca...@ua.pt wrote: I have the following dataframe: firm-c(rep(1:3,4)) year-c(rep(2001:2003,4)) X1-rep(c(10,NA),6) X2-rep(c(5,NA,2),4) data-data.frame(firm, year,X1,X2) data So I want to obtain the same dataframe with a variable X3

Re: [R] if else

2009-06-08 Thread Stavros Macrakis
On Mon, Jun 8, 2009 at 3:36 PM, Don MacQueen m...@llnl.gov wrote: Though I do agree that the way you've written the general case with any/ is.na and sum/na.rm is cleaner and clearer because more general, I don't agree at all with what you say about nested ifelse's vs. a series of assignments:

Re: [R] Splicing factors without losing levels

2009-06-09 Thread Stavros Macrakis
Various people have provided technical solutions to your problem. May I suggest, though, that 'splice' isn't quite the right word for this operation? Splicing two pieces of rope / movie film / audio tape / wires / etc. means connecting them at their ends, either at an extremity or in the middle,

Re: [R] Splicing factors without losing levels

2009-06-09 Thread Stavros Macrakis
On Tue, Jun 9, 2009 at 11:16 AM, Titus von der Malsburg malsb...@gmail.comwrote: On Tue, Jun 09, 2009 at 11:04:03AM -0400, Stavros Macrakis wrote: This may seem like a minor point, but I think it is worthwhile using descriptive names for functions. Makes sense. I thought I've seen

Re: [R] using regular expressions to retrieve a digit-digit-dot structure from a string

2009-06-09 Thread Stavros Macrakis
On Tue, Jun 9, 2009 at 7:44 AM, Mark Heckmann mark.heckm...@gmx.de wrote: Thanks for your help. Your answers solved the problem I posted and that is just when I noticed that I misspecified the problem ;) My problem is to separate a German texts by sentences. Unfortunately I haven't found an R

[R] Tables without names

2009-06-11 Thread Stavros Macrakis
A table without names displays like a vector: unname(table(2:3)) [1] 1 1 1 and preserves the table class (as with unname in general): dput(unname(table(2:3))) structure(c(1L, 1L), .Dim = 2L, class = table) Does that make sense? R is not consistent in its treatment of such

Re: [R] Tables without names

2009-06-12 Thread Stavros Macrakis
On Fri, Jun 12, 2009 at 6:09 AM, Duncan Murdoch murd...@stats.uwo.cawrote: On 11/06/2009 5:35 PM, Stavros Macrakis wrote: A table without names displays like a vector: unname(table(2:3)) [1] 1 1 1 and preserves the table class (as with unname in general): dput(unname(table(2

Re: [R] function inside ifelse

2009-06-15 Thread Stavros Macrakis
Of course functions can be used inside ifelse. They should return vectors. Be careful of the effect of recycling: ifelse(c(F,T,F,T,F,T),1:3,10:20) [1] 10 2 12 1 14 3 with functions: f- function(x) x/mean(x) ifelse(c(F,T,F,T,F,T),sqrt(1:3),f(10:20)) [1] 0.667 1.4142136 0.800

Re: [R] Referencing data frames

2009-06-15 Thread Stavros Macrakis
On Mon, Jun 15, 2009 at 12:38 PM, Payam Minoofar payam.minoo...@meissner.com wrote: ...I would like to have a function acquire an object by reference, and within the function create new objects based on the original object and then use the name of the original object as the base for the names

Re: [R] tapply with cbinded x

2009-06-16 Thread Stavros Macrakis
On Tue, Jun 16, 2009 at 5:16 AM, Stefan Uhmann stefan.uhm...@googlemail.com wrote: why does this not work? df - data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7), fac = c('A', 'A', 'B')) tapply(cbind(df$var1, df$var2, df$var3), df$fac, mean) Because tapply is defined for

Re: [R] [Rd] Floating point precision / guard digits? (PR#13771)

2009-06-20 Thread Stavros Macrakis
(I am replacing R-devel and r-bugs with r-help as addressees.) On Sat, Jun 20, 2009 at 9:45 AM, Dr. D. P. Kreil dpkr...@gmail.com wrote: So if I request a calculation of 0.3-0.1-0.1-0.1 and I do not get 0, that is not an issue of rounding / underflow (or whatever the correct technical term

Re: [R] [Rd] Floating point precision / guard digits? (PR#13771)

2009-06-20 Thread Stavros Macrakis
On Sat, Jun 20, 2009 at 4:10 PM, Dr. D. P. Kreil dpkr...@gmail.com wrote: Ah, that's probably where I went wrong. I thought R would take the 0.1, the 0.3, the 3, convert them to extended precision binary representations, do its calculations, an the reduction to normal double precision binary

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread Stavros Macrakis
On Sun, Mar 15, 2009 at 4:12 PM, diegol diego...@gmail.com wrote: ...This could be done in Excel much tidier in my opinion (especially the range_aux part), element by element (cell by cell)... If you'd do it element-by-element in Excel, why not do it element-by-element in R? Create a table

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread Stavros Macrakis
Using cut/split seems like gross overkill here. Among other things, you don't need to generate labels for all the different ranges. which(x=range)[1] seems straightforward enough to me, but you could also use the built-in function findInterval. -s

Re: [R] Testing for Inequality à la select case

2009-03-16 Thread Stavros Macrakis
functions rather. I agree that which(x=range)[1] is straighforward, but using such expression will require a loop to pull the trick, which I don't intend. Am I missing something? Regards, Diego Stavros Macrakis-2 wrote: Using cut/split seems like gross overkill here. Among other things

Re: [R] permutations in R

2009-03-16 Thread Stavros Macrakis
Greg, Thanks for helping this user. I assume you mean the permn function in the combinat package? For a new user (including me), it is not obvious how to get from the permutations function in the Combinations package to that. I see there is also a function gtools::permutations. The gtools

Re: [R] permutations in R

2009-03-17 Thread Stavros Macrakis
On Tue, Mar 17, 2009 at 5:24 AM, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Mon, 2009-03-16 at 18:43 -0400, Stavros Macrakis wrote: ... no way to find relevant functions, and no way of knowing which one to use if there is more than one. That is what the Task Views are meant to address

Re: [R] permutations in R

2009-03-17 Thread Stavros Macrakis
On Tue, Mar 17, 2009 at 12:41 PM, Greg Snow greg.s...@imail.org wrote: No, I meant the Combinations package, it is apparently an Omegahat package (http://www.omegahat.org/Combinations/).  It looks similar to the permn function as far as the usage goes, but the documentation includes

Re: [R] nth root

2009-03-19 Thread Stavros Macrakis
Matlab's Nthroot calculates the real nth root. For positive a, you can use a^(1/b); for negative a, b must be odd for the result to be real, and you can use -abs(a)^(1/b). So you could write: nthroot - function(a,b) ifelse( b %% 2 == 1 | a = 0, sign(a)*abs(a)^(1/b), NaN) This is a so-called

Re: [R] Creating dataframe names on the fly?

2009-03-21 Thread Stavros Macrakis
On Fri, Mar 20, 2009 at 8:18 PM, science! karthik@gmail.com wrote: I am aware that it is easily possible to create var names on the fly. e.g. assign(paste(m,i,sep=),j) but is it possible to assign dataframes to variables created on the fly? e.g. If I have a dataframe called master and I

Re: [R] Doing %o% that operates on columns instead of atomics

2009-03-25 Thread Stavros Macrakis
Maybe something like the code below? There is surely a more elegant way of handling the indices, maybe with plyr? -s xx - array(1:6,c(3,2)); xx [,1] [,2] [1,]14 [2,]25 [3,]36 yy - array(1:12,c(3,4)); yy [,1] [,2] [,3] [,4] [1,]147 10

Re: [R] Sorting problem

2009-03-28 Thread Stavros Macrakis
On Sat, Mar 28, 2009 at 7:53 AM, Duncan Murdoch murd...@stats.uwo.ca wrote: ...More generally, the xtfrm() function converts a vector into a numeric one that sorts in the same order. ... Thanks, I learn a lot just by reading the answers to other people's questions on this list. Some followup

Re: [R] Sorting problem

2009-03-29 Thread Stavros Macrakis
On Sun, Mar 29, 2009 at 6:15 AM, Duncan Murdoch murd...@stats.uwo.ca wrote: On 28/03/2009 4:57 PM, Stavros Macrakis wrote: On Sat, Mar 28, 2009 at 7:53 AM, Duncan Murdoch murd...@stats.uwo.ca wrote: 1) Where does the name 'xtfrm' come from? I don't know. Hmm. If the origins of the name

Re: [R] scope of variables in R

2009-03-31 Thread Stavros Macrakis
On Tue, Mar 31, 2009 at 7:10 AM, mau...@alice.it wrote: I need to allocate (using C nomenclature) a set of global variables, some integer scalars and some double vectors. ... My question is: how can I have R interpreter allocate globalvariables visible and accessible by all the functions in

Re: [R] target of assignment expands to non-language object

2009-03-31 Thread Stavros Macrakis
On Tue, Mar 31, 2009 at 10:05 AM, Alina Sheyman alina...@gmail.com wrote: I'm running the following code numbers - 1:50 for (i in 1:50) assign(paste(model,numbers[i]),i)-(lm(temp$Overall.Scaled.Score~temp$raw.score)) a) What is the purpose of numbers? Why not write paste(model,i) b)

Re: [R] scope of variables in R

2009-03-31 Thread Stavros Macrakis
On Tue, Mar 31, 2009 at 3:41 PM, Wacek Kusnierczyk waclaw.marcin.kusnierc...@idi.ntnu.no wrote: Stavros Macrakis wrote: ...All that being said, programming with global variables makes certain classes of bug much more likely ... in a language like r, that heavily relies on setting

[R] Definition of = vs. -

2009-04-01 Thread Stavros Macrakis
NOTA BENE: This email is about `=`, the assignment operator (e.g. {a=1} which is equivalent to { `=`(a,1) } ), not `=` the named-argument syntax (e.g. f(a=1), which is equivalent to eval(structure(quote(f(1)),names=c('','a'))). As far as I can tell from the documentation, assignment with = is

Re: [R] Definition of = vs. -

2009-04-01 Thread Stavros Macrakis
On Wed, Apr 1, 2009 at 10:55 AM, Duncan Murdoch murd...@stats.uwo.cawrote: On 4/1/2009 10:38 AM, Stavros Macrakis wrote: As far as I can tell from the documentation, assignment with = is precisely equivalent to assignment with -. Yet they call different primitives: The parser does treat

Re: [R] SUM,COUNT,AVG

2009-04-06 Thread Stavros Macrakis
There are various ways to do this in R. # sample data dd - data.frame(a=1:10,b=sample(3,10,replace=T),c=sample(3,10,replace=T)) Using the standard built-in functions, you can use: *** aggregate *** aggregate(dd,list(b=dd$b,c=dd$c),sum) b c a b c 1 1 1 10 2 2 2 2 1 3 2 1 *** tapply

Re: [R] SUM,COUNT,AVG

2009-04-06 Thread Stavros Macrakis
5 1 3 1 5 5.00 6 3 3 3 17 5.67 Hope this helps, -s On Mon, Apr 6, 2009 at 10:34 AM, Stavros Macrakis macra...@alum.mit.eduwrote: There are various ways to do this in R. # sample data dd - data.frame(a=1:10,b=sample(3,10,replace=T),c=sample(3,10,replace=T

[R] dput and getOptions('width')

2009-04-06 Thread Stavros Macrakis
dput appears to ignore the value of getOptions('width') Is there some other way to control the line width it uses? or to get it to observe getOptions('width')? If I wanted infinite line width, I could send the output to a textConnection and paste the pieces together, but putting line breaks back

Re: [R] Sequences

2009-04-07 Thread Stavros Macrakis
On Tue, Apr 7, 2009 at 8:13 AM, Melissa2k9 m.mcquil...@lancaster.ac.ukwrote: I am trying to make a sequence and am using a for loop for this. I want to start off with an initial value ie S[0]=0 then use the loop to create other values. This is what I have so far but I just keep getting error

Re: [R] Sequences

2009-04-07 Thread Stavros Macrakis
[mailto:r-help-boun...@r-project.org] On Behalf Of Stavros Macrakis Sent: Tuesday, April 07, 2009 9:11 AM To: Melissa2k9 Cc: r-help@r-project.org Subject: Re: [R] Sequences On Tue, Apr 7, 2009 at 8:13 AM, Melissa2k9 m.mcquil...@lancaster.ac.ukwrote: I am trying to make a sequence and am

Re: [R] extract argument names

2009-04-07 Thread Stavros Macrakis
a1 - qss(x1,lambda=100) parse(text=a1)[[1]][[2]] x1 This will not work for length(a) != 1, so you have to explicitly map over your list, e.g. a - paste(qss(,paste(x,1:6,sep = ) ,, lambda =100), sep = ) dput( lapply(a,function(x)parse(text=x)[[1]][[2]]) ) list(x1, x2, x3, x4, x5, x6)

Re: [R] numbers not behaving as expected

2009-04-09 Thread Stavros Macrakis
On Thu, Apr 9, 2009 at 1:39 PM, steve_fried...@nps.gov wrote: I have a data.frame Cell.ave (attached and created via dput(Cell.ave, Cell.ave) I'm afraid your attachment didn't make it into the r-help mail. Mailing list policy forbids binary attachments other than PS and PDF, but should be

Re: [R] numbers not behaving as expected

2009-04-09 Thread Stavros Macrakis
On Thu, Apr 9, 2009 at 2:05 PM, steve_fried...@nps.gov wrote: WetMonths - Cell.ave[Cell.ave$month  = 5 and Cell.ave$month = 11,] Error: unexpected symbol in WetMonths - Cell.ave[Cell.ave$month  = 5 and  Cell.ave$month = 11,] a) you are comparing with the *string* 5, not the *number* 5 (as I

[R] Using trace

2009-04-12 Thread Stavros Macrakis
I would like to trace functions, displaying their arguments and return value, but I haven't been able to figure out how to do this with the 'trace' function. After some thrashing, I got as far as this: fact - function(x) if(x1) 1 else x*fact(x-1) tracefnc - function()

Re: [R] Physical Units in Calculations

2009-04-13 Thread Stavros Macrakis
On Sun, Apr 12, 2009 at 11:01 PM, bill.venab...@csiro.au wrote: It is, however, an interesting problem and there are the tools there to handle it.  Basically you need to create a class for each kind of measure you want to handle (length, area, volume, weight, and so on) and then overload

Re: [R] Finding the 5th percentile

2009-04-13 Thread Stavros Macrakis
quantile( dsamp100, 0.05 ) On Mon, Apr 13, 2009 at 10:41 AM, Henry Cooper henry.1...@hotmail.co.uk wrote: dsamp100-coef(100,39.83,5739,2869.1,49.44) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the

Re: [R] Concatenation, was Re: Physical Units in Calculations

2009-04-13 Thread Stavros Macrakis
On Mon, Apr 13, 2009 at 5:15 AM, Peter Dalgaard p.dalga...@biostat.ku.dk wrote: Stavros Macrakis wrote: ...c of two time differences is currently a numeric vector, losing its units (hours, days, etc.) completely. That's actually a generic feature/issue of c(). ... There is some potential

  1   2   3   >