[R] Basic data structures

2008-08-10 Thread Stavros Macrakis
do that? The naive way, substr(strlist,matches, ...) doesn't work, partly because the attr operator doesn't distribute over lists (I see why it can't, but...). Thanks in advance for your patience with these very elementary questions, -s Stavros Macrakis, Cambridge, MA

[R] Keeping track of memory usage

2009-08-20 Thread Stavros Macrakis
How can I determine how much memory a given piece of my code is allocating (directly or indirectly)? -- essentially, the space analogue of system.time, something like this: system.space( x <- rnorm(1) ) 1 Vcells system.space( for (i in 1:1000) x <- rnorm(1) ) 1

Re: [R] Is there a construct for conditional comment?

2009-08-21 Thread Stavros Macrakis
On Thu, Aug 20, 2009 at 1:27 PM, David Winsemius wrote: ... > But an extremely simple modification "succeeds": > >  if ( 0 ) {" >  commented with zero >  "} else {" >  commented with one >  "} > > Returns: > [1] "\ncommented with one\n" Yes, but of course that executes neither one nor the other.

Re: [R] Book on R programming

2009-08-31 Thread Stavros Macrakis
I recommend you skim the Chambers book at Google Books or Amazon before buying it as a guide to programming in R. It is a fascinating book, but is more a discursive reflection on the history and philosophy of R than a practical guide to programming in R. It certainly explains the rationale for ma

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 Turner 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 straightforwardly in simple str

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 http://www.

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. Pyrame 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 coerced from logical

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 wrote: > Stavros Macrakis wrote: >> On Thu, Jun 25, 2009 at 12:47 PM, Craig P. 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 Karlsson

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 wrote: > ... > 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 on this > functi

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 wrote: > Greetings, I have a vector of the form: > [10,8,1,3,0,

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 wrote: > Unlike Splus, R does not use T for TRUE. > > On Tue, Jul 7, 2009 at 6:05 PM, Michael wrote: >> Hi all, >> >> I've got the fo

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 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 error?

Re: [R] Trig functions strange results

2009-07-14 Thread Stavros Macrakis
On Tue, Jul 14, 2009 at 1:45 PM, Nair, Murlidharan T 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 > Browse[1]>

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 mechanism

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 irritatio

Re: [R] quoting expressions in a list

2009-07-16 Thread Stavros Macrakis
On Thu, Jul 16, 2009 at 4:44 PM, Erik Iverson 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 = rnorm(

[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 na

Re: [R] Object equality for S4 objects

2009-07-30 Thread Stavros Macrakis
On Thu, Jul 30, 2009 at 12:01 PM, Martin Morgan 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)) returns TRUE. Thanks

Re: [R] Object equality for S4 objects

2009-07-30 Thread Stavros Macrakis
On Thu, Jul 30, 2009 at 4:03 PM, Martin Morgan 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-help@r-project.org m

[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 of

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

2009-11-11 Thread Stavros Macrakis
:40 PM, Richard M. Heiberger wrote: > 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? >>

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

2009-11-11 Thread Stavros Macrakis
solved the problem in R or ESS -s On Wed, Nov 11, 2009 at 8:43 PM, RICHARD M. HEIBERGER wrote: > On Wed, Nov 11, 2009 at 8:12 PM, Stavros Macrakis > wrote: > > Thanks for the suggestion. I'mm familiar with the truncate-lines > variable, > > but that'

[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` whi

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 wrote: > Just confirming it isn't the bug fixed in 2.11.0dev, and giving an even > simpler example: >

[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: D(qu

Re: [R] Beyond double-precision?

2009-05-11 Thread Stavros Macrakis
On Sat, May 9, 2009 at 12:17 PM, Berwin A Turlach 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) ... >  max(lx,ly) + log

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 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 save.imag

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 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-by-need sema

[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 fi

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

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

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

2009-05-16 Thread Stavros Macrakis
On Sat, May 16, 2009 at 9:11 AM, Aval Sarri 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 last e

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 wrote: > > Hi Guy, > > I am having trouble graphing the following function > > √2Γ(n/2)/[√n - 1Γ((n - 1)/2 for the values of n betw

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

[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
od for that. For > some examples see: > > methods(diff) > > > > On Mon, May 18, 2009 at 4:24 PM, Stavros Macrakis > wrote: > > I would like to apply a function 'f' to the lagged version of a vector > and > > the vector itself. > > > > This i

Re: [R] Generic 'diff'

2009-05-18 Thread Stavros Macrakis
ding your objection. -s > On Mon, May 18, 2009 at 5:48 PM, Stavros Macrakis > 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 &g

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 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 *alway

Re: [R] Functions returning functions

2009-05-20 Thread Stavros Macrakis
On Wed, May 20, 2009 at 7:21 AM, Paulo Grahl 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. Then I ca

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 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 > exceed this

[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 Analysi

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 wrote: > If you want the hours from a POSIXct, here is one way of doing it

Re: [R] Class for time of day?

2009-05-22 Thread Stavros Macrakis
On Thu, May 21, 2009 at 8:28 PM, Gabor Grothendieck 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 falls back to > the

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 n

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 wrote: > 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 manually > read the XM

[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 [[alter

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 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: df[ , nam

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

[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 . (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: odbcOpe

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 mis

[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 cer

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

Re: [R] maxtrix to permutation vector

2009-05-29 Thread Stavros Macrakis
)) > m y x x y a 1 3 b 2 4 > melt(m) x y 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,]

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 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 fact that the o

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

2009-06-01 Thread Stavros Macrakis
type of offending object was found. 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

Re: [R] R Solves Shakespeare Authorship Question

2009-06-01 Thread Stavros Macrakis
I don't know why this paper is getting our attention here -- it is just an undergraduate student term paper for a computer science course on statistics and data mining. -s On Mon, Jun 1, 2009 at 5:48 PM, wrote: > On Sat, 30-May-2009 at 07:42PM -0700, Arthur Burke wrote: > > |> Thos

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

2009-06-01 Thread Stavros Macrakis
On Mon, Jun 1, 2009 at 11:33 AM, Wacek Kusnierczyk < waclaw.marcin.kusnierc...@idi.ntnu.no> wrote: > ... there is an ugly lack of consistency here:... > I agree that it's inconsistent that 1:'2' --> 1:2 # this doesn't seem to be documented in ? seq 1+ '2' --> error 1+facto

[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 n

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 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 by > a fac

Re: [R] if else

2009-06-08 Thread Stavros Macrakis
On Mon, Jun 8, 2009 at 1:48 PM, Cecilia Carmo 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 that is:

Re: [R] if else

2009-06-08 Thread Stavros Macrakis
On Mon, Jun 8, 2009 at 3:36 PM, Don MacQueen 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: > In my opinion

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 wrote: > 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'

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 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 package doing th

[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 wrote: > 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 unnam

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 1.0

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 n

Re: [R] tapply with cbinded x

2009-06-16 Thread Stavros Macrakis
On Tue, Jun 16, 2009 at 5:16 AM, Stefan Uhmann 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 atomic vectors and not

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 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 would be for th

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 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 floats w

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: > which(

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 Na 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 and no argument

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 1

Re: [R] Sorting problem

2009-03-28 Thread Stavros Macrakis
On Sat, Mar 28, 2009 at 7:53 AM, Duncan Murdoch 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 questions: 1) Wher

Re: [R] Sorting problem

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

Re: [R] Sorting problem

2009-03-29 Thread Stavros Macrakis
On Sun, Mar 29, 2009 at 5:21 PM, Ben Bolker wrote: >  Adding > > xtfrm.numeric <- function(x) {x} > > would seem to add the case that people are looking for -- unless > there's something special that needs to be handled with NAs ??? Yes, that was what I was suggesting. xtfrm currently converts N

Re: [R] scope of variables in R

2009-03-31 Thread Stavros Macrakis
On Tue, Mar 31, 2009 at 7:10 AM, 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 "global"variables > visible and accessible by all the functions in the same

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 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) Why a

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

[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 prec

Re: [R] Definition of = vs. <-

2009-04-01 Thread Stavros Macrakis
On Wed, Apr 1, 2009 at 10:55 AM, Duncan Murdoch wrote: > 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: >&g

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.00 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 wrote: > 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,

[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 wrote: > > 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 messages. > R only all

Re: [R] Sequences

2009-04-07 Thread Stavros Macrakis
an R-centric in detail).  I think it's fair to say that the man pages do > not provide all of the details, either. As a result, I still occasionally > get bitten by these indexing subtleties (which, of course, may just be due > to my dummheit). > > Cheers, > Bert > >

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,

Re: [R] numbers not behaving as expected

2009-04-09 Thread Stavros Macrakis
On Thu, Apr 9, 2009 at 1:39 PM, 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 forwarding plaintext at

Re: [R] numbers not behaving as expected

2009-04-09 Thread Stavros Macrakis
On Thu, Apr 9, 2009 at 2:05 PM, 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 men

[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(x<1) 1 else x*fact(x-1) tracefnc <- function() dput(as.list(pare

Re: [R] Physical Units in Calculations

2009-04-13 Thread Stavros Macrakis
On Sun, Apr 12, 2009 at 11:01 PM, 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 the arithmet

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 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 posting guide http://www

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 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 s

Re: [R] Automating object creation

2009-04-14 Thread Stavros Macrakis
It is certainly possible to create x2, x4, etc. using something like assign( sprintf("x%d",i), ...value... ). But are you sure you need separate *variables* x2, x4, etc.? Why not create a list of vectors addressible as x[2] etc.? You can do that with x <- list() (to define the data type of x as

Re: [R] Forcing the extrapolation of loess through the origin

2009-04-14 Thread Stavros Macrakis
On Tue, Apr 14, 2009 at 1:08 PM, wrote: > I'm fitting a line to my dataset. Later I want to predict missing values that > exceed the [min,max] interval of my empirical data, therefore I choose > surface="direct" for extrapolation. > > l1<-loess(y1~x1,span=0.1,data.frame(x=x1,y=y1),control=loess

Re: [R] Intersection of two sets of intervals

2009-04-15 Thread Stavros Macrakis
There is a very nice "intervals" package in CRAN. It is impressively efficient even for intersections of many millions of intervals. If I remember correctly, it is purely in-core, so on a 32-bit R you'll be limited to something like 100 million intervals. Is that enough for your application?

  1   2   3   >