[R] adding class attributes to strings: works in a loop, but not directly

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed May 29 16:34:06 CEST 2013


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.



More information about the R-help mailing list