Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is attributes() a function in R?

Tags:

r

Help files call attributes() a function. Its syntax looks like a function call. Even class(attributes) calls it a function.

But I see I can assign something to attributes(myobject), which seems unusual. For example, I cannot assign anything to log(myobject).

So what is the proper name for "functions" like attributes()? Are there any other examples of it? How do you tell them apart from regular functions? (Other than trying supposedfunction(x)<-0, that is.)

Finally, I guess attributes() implementation overrides the assignment operator, in order to become a destination for assignments. Am I right? Is there any usable guide on how to do it?

like image 403
nvja Avatar asked Aug 24 '26 16:08

nvja


1 Answers

Very good observation Indeed. It's an example of replacement function, if you see closely and type apropos('attributes') in your R console, It will return

 "attributes"                        
 "attributes<-"  

along with other outputs.

So, basically the place where you are able to assign on the left sign of assignment operator, you are not calling attributes, you are actually calling attributes<- , There are many functions in R like that for example: names(), colnames(), length() etc. In your example log doesn't have any replacement counterpart hence it doesn't work the way you anticipated.

Definiton(from advanced R book link given below):

Replacement functions act like they modify their arguments in place, and have the special name xxx<-. They typically have two arguments (x and value), although they can have more, and they must return the modified object

If you want to see the list of these functions you can do : apropos('<-$') and you can check out similar functions, which has similar kind of properties.

You can read about it here and here

I am hopeful that this solves your problem.

like image 184
PKumar Avatar answered Aug 26 '26 05:08

PKumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!