[R] Set attributes for object known by name

Marc Girondot m@rc_grt @ending from y@hoo@fr
Thu Oct 11 06:15:26 CEST 2018


Hello everybody,

Has someone the solution to set attribute when variable is known by name ?

Thanks a lot

Marc

Let see this exemple:

# The variable name is stored as characters.

varname <- "myvarname"
assign(x = varname, data.frame(A=1:5, B=2:6))
attributes(myvarname)

$names
[1] "A" "B"
$class
[1] "data.frame"
$row.names
[1] 1 2 3 4 5

# perfect

attributes(get(varname))

# It works also

$names
[1] "A" "B"
$class
[1] "data.frame"

$row.names
[1] 1 2 3 4 5

attributes(myvarname)$NewAtt <- "MyAtt"

# It works

attributes(get(varname))$NewAtt2 <- "MyAtt2"
Error in attributes(get(varname))$NewAtt2 <- "MyAtt2" :
   impossible de trouver la fonction "get<-"

# Error...



More information about the R-help mailing list