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

Blaser Nello nblaser at ispm.unibe.ch
Wed May 29 15:55:44 CEST 2013


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.



More information about the R-help mailing list