Hi How do I specify an S4 class with a slot that is potentially numeric, but NA by default? I want the slot to be NA until I calculate its value (an expensive operation, not needed for all applications). When its value is known, I will create a new object with the correct value inserted in the slot. I want "NA" to signify "not known". My attempt fails because NA is not numeric: > setClass ("foo",representation=representation(x="numeric"),prototype=list(x=NA)) Error in makePrototypeFromClassDef(properties, ClassDef, immediate, where) : in making the prototype for class "foo" elements of the prototype failed to match the corresponding slot class: x (class ?numeric? ) > (the real application has other slots too). I can use "NaN", which is numeric: > setClass ("foo",representation=representation(x="numeric"),prototype=list(x=NaN)) [1] "foo" > But this is not the correct sense: to me "NaN" means "not a number" and I want the sense to be "not available". Any advice? -- Robin Hankin Uncertainty Analyst and Neutral Theorist, National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
On Wednesday 26 March 2008 12:04:11 pm Robin Hankin wrote:> Hi > > How do I specify an S4 class with a slot that is potentially numeric, > but NA > by default? I want the slot to be NA until I calculate its value > (an expensive operation, not needed for all applications). When > its value is > known, I will create a new object with the correct value inserted in > the slot. > > I want "NA" to signify "not known". > > My attempt fails because NA is not numeric: >Try as.numeric(NA) - by default, plain NA is of type "logical" best Vladimir Dergachev> > > > -- > Robin Hankin > Uncertainty Analyst and Neutral Theorist, > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Vladimir Dergachev RCG Ardis Capital LLC
On Wed, 26 Mar 2008, Robin Hankin wrote:> Hi > > How do I specify an S4 class with a slot that is potentially numeric, > but NA > by default? I want the slot to be NA until I calculate its value > (an expensive operation, not needed for all applications). When > its value is > known, I will create a new object with the correct value inserted in > the slot. > > I want "NA" to signify "not known". > > My attempt fails because NA is not numeric:setClass("foo", representation=representation(x="numeric"), prototype=list(x=as.numeric(NA))) new("foo") is OK, we have a character NA prototype in an sp CRS class, and it's never been any trouble. Roger> > > > setClass > ("foo",representation=representation(x="numeric"),prototype=list(x=NA)) > Error in makePrototypeFromClassDef(properties, ClassDef, immediate, > where) : > in making the prototype for class "foo" elements of the prototype > failed to match the corresponding slot class: x (class ?numeric? ) > > > > (the real application has other slots too). I can > use "NaN", which is numeric: > > > > setClass > ("foo",representation=representation(x="numeric"),prototype=list(x=NaN)) > [1] "foo" > > > > But this is not the correct sense: to me "NaN" means "not a number" > and I want > the sense to be "not available". > > > > Any advice? > > > > > > -- > Robin Hankin > Uncertainty Analyst and Neutral Theorist, > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
Hi Robin, you could try this:> setClass("foo",representation=representation(x="numeric"),prototype=list(x=as.numeric(NA)))> test<-new("foo") > test >An object of class "foo" >Slot "x": >[1] NACheers, Christian Robin Hankin wrote:> Hi > > How do I specify an S4 class with a slot that is potentially numeric, > but NA > by default? I want the slot to be NA until I calculate its value > (an expensive operation, not needed for all applications). When > its value is > known, I will create a new object with the correct value inserted in > the slot. > > I want "NA" to signify "not known". > > My attempt fails because NA is not numeric: > > > > setClass > ("foo",representation=representation(x="numeric"),prototype=list(x=NA)) > Error in makePrototypeFromClassDef(properties, ClassDef, immediate, > where) : > in making the prototype for class "foo" elements of the prototype > failed to match the corresponding slot class: x (class ?numeric? ) > > > > (the real application has other slots too). I can > use "NaN", which is numeric: > > > > setClass > ("foo",representation=representation(x="numeric"),prototype=list(x=NaN)) > [1] "foo" > > > > But this is not the correct sense: to me "NaN" means "not a number" > and I want > the sense to be "not available". > > > > Any advice? > > > > > > -- > Robin Hankin > Uncertainty Analyst and Neutral Theorist, > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Christian Kohler Institute of Functional Genomics Computational Diagnostics University of Regensburg (BioPark I) D-93147 Regensburg (Germany) Tel. +49 941 943 5055 Fax +49 941 943 5020 christian.kohler at klinik.uni-regensburg.de
On Mar 26, 2008, at 12:04 PM, Robin Hankin wrote:> Hi > > How do I specify an S4 class with a slot that is potentially numeric, > but NA > by default? I want the slot to be NA until I calculate its value > (an expensive operation, not needed for all applications). When > its value is > known, I will create a new object with the correct value inserted in > the slot. > > I want "NA" to signify "not known". > > My attempt fails because NA is not numeric: > >> > setClass > ("foo > ",representation=representation(x="numeric"),prototype=list(x=NA)) > Error in makePrototypeFromClassDef(properties, ClassDef, immediate, > where) : > in making the prototype for class "foo" elements of the prototype > failed to match the corresponding slot class: x (class ?numeric? ) >> >Why don't you just initialize x with the correct type then? NA is logical, so either use as.numeric(NA) or NA_real_ (see ?NA) Cheers, S> (the real application has other slots too). I can > use "NaN", which is numeric: > >> > setClass > ("foo > ",representation=representation(x="numeric"),prototype=list(x=NaN)) > [1] "foo" >> > > But this is not the correct sense: to me "NaN" means "not a number" > and I want > the sense to be "not available". > > > > Any advice? > > > > > > -- > Robin Hankin > Uncertainty Analyst and Neutral Theorist, > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >
Robin Hankin wrote:> Hi > > How do I specify an S4 class with a slot that is potentially numeric, > but NA > by default? I want the slot to be NA until I calculate its value > (an expensive operation, not needed for all applications). When > its value is > known, I will create a new object with the correct value inserted in > the slot. > > I want "NA" to signify "not known". > > My attempt fails because NA is not numeric: > > > > setClass > ("foo",representation=representation(x="numeric"),prototype=list(x=NA)) > Error in makePrototypeFromClassDef(properties, ClassDef, immediate, > where) : > in making the prototype for class "foo" elements of the prototype > failed to match the corresponding slot class: x (class ?numeric? ) > > > > (the real application has other slots too). I can > use "NaN", which is numeric: > > > > setClass > ("foo",representation=representation(x="numeric"),prototype=list(x=NaN)) > [1] "foo" > > > > But this is not the correct sense: to me "NaN" means "not a number" > and I want > the sense to be "not available". > > > > Any advice? > > > >How about NA_real_ or NA_integer_? -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907