Franckx Laurent
2013-May-29 11:49 UTC
[R] adding class attributes to strings: works in a loop, but not directly
Dear all I try to assign class attributes to strings. Depending on the approach I use, it works (including method dispatch) or fails. Let me clarify with an example. Let:> AONtptmodelist <- c("FOOT","BICY")When I assign "class" attributes directly to these strings, it fails:> class("BICY") <- "AONmode"Error in class("BICY") <- "AONmode" : target of assignment expands to non-language object However, when I assign the attributes in a loop, it does work:> for(tptmode in AONtptmodelist) {+ class(tptmode) <- "AONmode" + cat("The value of is.object(tptmode) for " , tptmode , "is: " , is.object(tptmode) , ".\n") + cat("The class of " , tptmode , "is: " , class(tptmode) , ".\n") + } The value of is.object(tptmode) for FOOT is: TRUE . The class of FOOT is: AONmode . The value of is.object(tptmode) for BICY is: TRUE . The class of BICY is: AONmode . Moreover, within this loop, method dispatch works correctly. However, when I start a new loop over the same vector, I get:> for(tptmode in AONtptmodelist) {+ cat("The value of is.object(tptmode) for " , tptmode , "is: " , is.object(tptmode) , ".\n") + cat("The class of " , tptmode , "is: " , class(tptmode) , ".\n") + } The value of is.object(tptmode) for FOOT is: FALSE . The class of FOOT is: character . The value of is.object(tptmode) for BICY is: FALSE . The class of BICY is: character . I find this troublesome for two reasons. First, I do not understand the difference between the two approaches. Why can I assign a class attribute to a string when it is called in a loop, but not directly? And why is the class attribution not permanent? Second, the problem was concealed until now precisely because I assigned the attributes in a link. However, I would like to centralise my class definition in one single place in my code, thus outside the loop where the methods are called. Laurent Franckx, PhD VITO NV Boeretang 200, 2400 MOL, Belgium Tel. + 32 14 33 58 22 Skype: laurent.franckx laurent.franckx at vito.be Visit our website: www.vito.be/english and http://www.vito.be/transport [http://www.vito.be/e-maildisclaimer/vito.png] Ontdek hoe VITO de transitie naar een duurzame maatschappij op gang trekt: www.vito.be/duurzaamheidsverslag2012<http://www.vito.be/duurzaamheidsverslag2012> VITO Disclaimer: http://www.vito.be/e-maildisclaimer
Blaser Nello
2013-May-29 13:55 UTC
[R] adding class attributes to strings: works in a loop, but not directly
There are two separate issues that seem to be unclear. Concerning the class assignment: You cannot assign a class to a character string. You can assign a class to an object that contains a character string:> a <- "b"# ok> class(a) <- "AONmode"# not ok> class("b") <- "AONmode"Error in class("b") <- "AONmode" : target of assignment expands to non-language object Concerning your for-loop: for (tptmode in AONtptmodelist) creates a new object tptmode and changes this. This will in no way alter your object AONtptmodelist. If you want to have the elements of AONtptmodelist have a different class, you could for instance create a new object newAONtptmodelist as follows: newAONtptmodelist <- lapply(AONtptmodelist, function(x){ class(x) <- "AONmode" return(x) }) Best, Nello -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Franckx Laurent Sent: Mittwoch, 29. Mai 2013 13:49 To: 'r-help at r-project.org' Subject: [R] adding class attributes to strings: works in a loop, but not directly Dear all I try to assign class attributes to strings. Depending on the approach I use, it works (including method dispatch) or fails. Let me clarify with an example. Let:> AONtptmodelist <- c("FOOT","BICY")When I assign "class" attributes directly to these strings, it fails:> class("BICY") <- "AONmode"Error in class("BICY") <- "AONmode" : target of assignment expands to non-language object However, when I assign the attributes in a loop, it does work:> for(tptmode in AONtptmodelist) {+ class(tptmode) <- "AONmode" + cat("The value of is.object(tptmode) for " , tptmode , "is: " , + is.object(tptmode) , ".\n") cat("The class of " , tptmode , "is: " , + class(tptmode) , ".\n") } The value of is.object(tptmode) for FOOT is: TRUE . The class of FOOT is: AONmode . The value of is.object(tptmode) for BICY is: TRUE . The class of BICY is: AONmode . Moreover, within this loop, method dispatch works correctly. However, when I start a new loop over the same vector, I get:> for(tptmode in AONtptmodelist) {+ cat("The value of is.object(tptmode) for " , tptmode , "is: " , + is.object(tptmode) , ".\n") cat("The class of " , tptmode , "is: " , + class(tptmode) , ".\n") } The value of is.object(tptmode) for FOOT is: FALSE . The class of FOOT is: character . The value of is.object(tptmode) for BICY is: FALSE . The class of BICY is: character . I find this troublesome for two reasons. First, I do not understand the difference between the two approaches. Why can I assign a class attribute to a string when it is called in a loop, but not directly? And why is the class attribution not permanent? Second, the problem was concealed until now precisely because I assigned the attributes in a link. However, I would like to centralise my class definition in one single place in my code, thus outside the loop where the methods are called. Laurent Franckx, PhD VITO NV Boeretang 200, 2400 MOL, Belgium Tel. + 32 14 33 58 22 Skype: laurent.franckx laurent.franckx at vito.be Visit our website: www.vito.be/english and http://www.vito.be/transport [http://www.vito.be/e-maildisclaimer/vito.png] Ontdek hoe VITO de transitie naar een duurzame maatschappij op gang trekt: www.vito.be/duurzaamheidsverslag2012<http://www.vito.be/duurzaamheidsver slag2012> VITO Disclaimer: http://www.vito.be/e-maildisclaimer ______________________________________________ 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.
Jeff Newmiller
2013-May-29 14:34 UTC
[R] adding class attributes to strings: works in a loop, but not directly
a) You cannot assign attributes to literal values such as "BICY". You must assign them to variables, as in x <- "BICY" class(x) <- "AONmode" b) You cannot give the separate elements of a vector their own separate attributes unless the vector is of mode "list". Ordinarily, attributes apply to the whole vector. (When the vector is of mode "list" then each element may be a vector on its own with its own attributes.) y <- c("FOOT","BICY") class(y) <- "AONmode" z <- vector( "list", 2 ) z[[1]] <- "FOOT" class( z[[1]] ) <- "AONmode" z[[2]] <- "BICY" class( z[[2]] ) <- "AONmode" --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Franckx Laurent <laurent.franckx at vito.be> wrote:>Dear all > >I try to assign class attributes to strings. Depending on the approach >I use, it works (including method dispatch) or fails. > >Let me clarify with an example. > >Let: > >> AONtptmodelist <- c("FOOT","BICY") > > >When I assign "class" attributes directly to these strings, it fails: > >> class("BICY") <- "AONmode" >Error in class("BICY") <- "AONmode" : > target of assignment expands to non-language object > >However, when I assign the attributes in a loop, it does work: > >> for(tptmode in AONtptmodelist) { >+ class(tptmode) <- "AONmode" >+ cat("The value of is.object(tptmode) for " , tptmode , "is: " , >is.object(tptmode) , ".\n") >+ cat("The class of " , tptmode , "is: " , class(tptmode) , ".\n") >+ } >The value of is.object(tptmode) for FOOT is: TRUE . >The class of FOOT is: AONmode . >The value of is.object(tptmode) for BICY is: TRUE . >The class of BICY is: AONmode . > >Moreover, within this loop, method dispatch works correctly. > >However, when I start a new loop over the same vector, I get: > >> for(tptmode in AONtptmodelist) { >+ cat("The value of is.object(tptmode) for " , tptmode , "is: " , >is.object(tptmode) , ".\n") >+ cat("The class of " , tptmode , "is: " , class(tptmode) , ".\n") >+ } >The value of is.object(tptmode) for FOOT is: FALSE . >The class of FOOT is: character . >The value of is.object(tptmode) for BICY is: FALSE . >The class of BICY is: character . > > >I find this troublesome for two reasons. First, I do not understand the >difference between the two approaches. Why can I assign a class >attribute to a string when it is called in a loop, but not directly? >And why is the class attribution not permanent? Second, the problem was >concealed until now precisely because I assigned the attributes in a >link. However, I would like to centralise my class definition in one >single place in my code, thus outside the loop where the methods are >called. > > > >Laurent Franckx, PhD >VITO NV >Boeretang 200, 2400 MOL, Belgium >Tel. + 32 14 33 58 22 >Skype: laurent.franckx >laurent.franckx at vito.be >Visit our website: www.vito.be/english and http://www.vito.be/transport > > > > > > > > > > > >[http://www.vito.be/e-maildisclaimer/vito.png] > > >Ontdek hoe VITO de transitie naar een duurzame maatschappij op gang >trekt: >www.vito.be/duurzaamheidsverslag2012<http://www.vito.be/duurzaamheidsverslag2012> > > >VITO Disclaimer: http://www.vito.be/e-maildisclaimer > >______________________________________________ >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.