Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using attr function dynamically

Considering the following data frame

df<-data.frame(a=c(1,2,3))

I can show it by doing:

df

or

get("df")

Also, I can give an attribute to it by doing this:

attr(df,"anyAttribute")<-"df attribute"
attributes(df)
$names
[1] "a"

$class
[1] "data.frame"

$row.names
[1] 1 2 3

$anyAttribute
[1] "df attribute"

Is there any way to give attributes to dataframes dynamically?. What I want is something like this:

> attr(get("df"),"anyAttribute")<-"df attribute"
Error in attr(get("df"), "anyAttribute") <- "df attribute" : 
  destino de la asignación se expande a un objeto fuera del lenguaje
like image 376
Lev Avatar asked Oct 28 '25 23:10

Lev


1 Answers

Do you mean this?

r <- `attr<-`(get("df"), "anyAttribute", "df attribute")

attributes(r)
# $names
# [1] "a"
# 
# $class
# [1] "data.frame"
# 
# $row.names
# [1] 1 2 3
# 
# $anyAttribute
# [1] "df attribute"
like image 181
jay.sf Avatar answered Oct 30 '25 14:10

jay.sf



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!