Search notes:

R function: attr

All objects can have named attributes whose values can be set and queried with attr().

Setting attributes

attr(obj, nam) <- val sets the attribute named nam in the object obj to val.
attr(obj, nam) returns the associated value of nam in obj.
obj <- c('Foo', 'Bar', 'Baz')

attr(obj, 'First attribute' ) <- "Value for 1st attribute"
attr(obj, 'Second attribute') <- 1:5

str(obj)
# atomic [1:3] Foo Bar Baz
# - attr(*, "First attribute")= chr "Value for 1st attribute"
# - attr(*, "Second attribute")= int [1:5] 1 2 3 4 5

cat("\n\n")
attributes(obj)
# $`First attribute`
# [1] "Value for 1st attribute"
# 
# $`Second attribute`
# [1] 1 2 3 4 5
Github repository about-r, path: /functions/attr/attr.R

Removing attributes

An attribute can be removed from an object by assigning the NULL object to an attribute:
three <- 3;
attr(three, 'A') <- 'foo';
attr(three, 'B') <- 'bar';
attr(three, 'C') <- 'baz';

attr(three, 'B') <- NULL;
attributes(three);
#
# $A
# [1] "foo"
# 
# $C
# [1] "baz"
Github repository about-r, path: /functions/attr/remove-attribute.R

See also

Function attributes()
Index to (some) R functions

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745457889, '18.117.157.139', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/R/functions/attr(90): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78