From: William Dunlap <wdunlap_at_tibco.com>

Date: Fri, 27 May 2011 08:18:46 -0700

[1] 1 2 4

> omitZero(c(TRUE,TRUE,FALSE))

Error in UseMethod("omitZero") :

no applicable method for 'omitZero' applied to an object of class "logical"

> omitZero(data.frame(logical=c(TRUE,TRUE,FALSE))) Error in UseMethod("omitZero") :

no applicable method for 'omitZero' applied to an object of class "data.frame"

> omitZero(c(one=1,zero=0,five=5))

one five

1 5

> omitZero(matrix(0:3,2,2))

[1] 1 2 3

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 27 May 2011 - 15:23:08 GMT

Date: Fri, 27 May 2011 08:18:46 -0700

*> -----Original Message-----
**> From: Duncan Murdoch [mailto:murdoch.duncan_at_gmail.com]
**> Sent: Friday, May 27, 2011 5:23 AM
**> To: Albert-Jan Roskam
**> Cc: William Dunlap; Marc Schwartz; R Mailing List
**> Subject: Re: [R] NaN, Inf to NA
**>
*

> On 11-05-27 4:27 AM, Albert-Jan Roskam wrote:

*> > Aha! Thank you very much for that clarification! It would
**> be much more user
**> > friendly if R generated a NotImplementedError or something
**> similar. The 'garbage
**> > results' are pretty misleading, esp. to a novice.
**>
**> I think that's a good idea. The default methods are
**> documented to work
**> on atomic vectors; dataframes are not atomic vectors, so it would be
**> reasonable to generate an error. (See ?is.atomic for a definition of
**> atomic vectors.)
**>
**> I'll see if this causes a lot of trouble...
**>
**> Duncan Murdoch
*

One of my beefs about the S3 and S4 object systems is that the "default" or "ANY" method is often used for both the the simple (atomic, matrix of atomic, named atomic, etc.) classes and for any class the writer hasn't thought of. It can be difficult to write code that works well in both cases.

I've tried just omitting the default method to avoid the problem. I end up writing a lot of identical methods for the the simple cases, but I get the error checking that I want. E.g.,

> omitZero <- function(x) UseMethod("omitZero") > omitZero.numeric <- function(x)x[x!=0] > omitZero.integer <- function(x)x[x!=0] > omitZero.complex <- function(x)x[x!=0] > omitZero(c(1L,2L,0L,4L))

[1] 1 2 4

> omitZero(c(TRUE,TRUE,FALSE))

Error in UseMethod("omitZero") :

no applicable method for 'omitZero' applied to an object of class "logical"

> omitZero(data.frame(logical=c(TRUE,TRUE,FALSE))) Error in UseMethod("omitZero") :

no applicable method for 'omitZero' applied to an object of class "data.frame"

> omitZero(c(one=1,zero=0,five=5))

one five

1 5

> omitZero(matrix(0:3,2,2))

[1] 1 2 3

In other situations the default case may work, perhaps slowly, for all classes but I'll add special code for the simple classes so they get dealt with more efficiently.

Bill Dunlap

Spotfire, TIBCO Software

wdunlap tibco.com

*>
**>
**> >
*

> > I wanted to recode every NaN and Inf value of an entire

*> data.frame to NA. The
**> > data.frame also includes character variables. So the
**> following might work (?)
**> > (Can't test it here)
**> >
**> > ditch<- function(x) ifelse(is.infinite(x) | is.nan(x), NA, x)
**> > df<- apply(df, 2, ditch)
**> >
**> >
**> >
**> >
**> >
**> > ________________________________
**> > From: William Dunlap<wdunlap_at_tibco.com>
**> >
**> > Cc: R Mailing List<r-help_at_r-project.org>
**> > Sent: Fri, May 27, 2011 12:57:01 AM
**> > Subject: RE: [R] NaN, Inf to NA
**> >
**> > I think the source of the OP's problem is that
**> > while things like df>30 and is.na(df) return
**> > a logical matrix with the dimensions of the
**> > data.frame df, both is.infinite(df) and is.nan(df)
**> > return a logical vector as long as the number
**> > of columns of df. (`>` and is.na have data.frame
**> > methods but is.infinite and is.nan do not: the latter
**> > give garbage results for data.frames.)
**> >
**> > Bill Dunlap
**> > Spotfire, TIBCO Software
**> > wdunlap tibco.com
**> >
**> >> -----Original Message-----
**> >> From: r-help-bounces_at_r-project.org
**> >> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Marc Schwartz
**> >> Sent: Thursday, May 26, 2011 2:15 PM
**> >> To: Albert-Jan Roskam
**> >> Cc: R Mailing List
**> >> Subject: Re: [R] NaN, Inf to NA
**> >>
**> >> On May 26, 2011, at 3:18 PM, Albert-Jan Roskam wrote:
**> >>
**> >>> Hi,
**> >>>
**> >>> I want to recode all Inf and NaN values to NA, but I;m
**> >> surprised to see the
**> >>> result of the following code. Could anybody enlighten me
**> >> about this?
**> >>>
**> >>>> df<- data.frame(a=c(NA, NaN, Inf, 1:3))
**> >>>> df[is.infinite(df) | is.nan(df)]<- NA
**> >>>> df
**> >>> a
**> >>> 1 NA
**> >>> 2 NaN
**> >>> 3 Inf
**> >>> 4 1
**> >>> 5 2
**> >>> 6 3
**> >>>>
**> >>>
**> >>>
**> >>> Thanks!
**> >>>
**> >>> Cheers!!
**> >>> Albert-Jan
**> >>
**> >>
**> >> The canonical way is to use is.na() to assign the NA value
**> >> based upon a condition. See ?is.na for more information.
**> >>
**> >> is.na(df$a)<- !is.finite(df$a)
**> >>
**> >>> df
**> >> a
**> >> 1 NA
**> >> 2 NA
**> >> 3 NA
**> >> 4 1
**> >> 5 2
**> >> 6 3
**> >>
**> >>
**> >> HTH,
**> >>
**> >> Marc Schwartz
**> >>
**> >> ______________________________________________
**> >> R-help_at_r-project.org mailing list
**> >> https://stat.ethz.ch/mailman/listinfo/r-help
**> >> PLEASE do read the posting guide
**> >> http://www.R-project.org/posting-guide.html
**> >> and provide commented, minimal, self-contained, reproducible code.
**> >>
**> >
**> > [[alternative HTML version deleted]]
**> >
**> > ______________________________________________
**> > R-help_at_r-project.org mailing list
**> > https://stat.ethz.ch/mailman/listinfo/r-help
**> > PLEASE do read the posting guide
**> http://www.R-project.org/posting-guide.html
**> > and provide commented, minimal, self-contained, reproducible code.
**>
**>
*

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 27 May 2011 - 15:23:08 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Fri 27 May 2011 - 15:50:10 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*