Set attributes for object known by name

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Set attributes for object known by name

R help mailing list-2
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...

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Set attributes for object known by name

plangfelder
I would try something like

x = get(myvarname)
attr(x, "foo") = "bar"
assign(varname, x)

HTH,

Peter

On Wed, Oct 10, 2018 at 9:15 PM Marc Girondot via R-help <
[hidden email]> wrote:

> 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...
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Set attributes for object known by name

plangfelder
oops, I think the right code would be

x = get(varname)
attr(x, "foo") = "bar"
assign(varname, x)

On Wed, Oct 10, 2018 at 9:30 PM Peter Langfelder <[hidden email]>
wrote:

> I would try something like
>
> x = get(myvarname)
> attr(x, "foo") = "bar"
> assign(varname, x)
>
> HTH,
>
> Peter
>
> On Wed, Oct 10, 2018 at 9:15 PM Marc Girondot via R-help <
> [hidden email]> wrote:
>
>> 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...
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Set attributes for object known by name

Bert Gunter-2
Well, it can be done without a temporary variable, but I'm not sure you
would want to.
Anyway...

## simplified example
> a <- 1
> vname <- "a"
> eval(substitute(attr(x,"b") <- "hi", list( x = as.name(vname))))
> a
[1] 1
attr(,"b")
[1] "hi"


Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Oct 10, 2018 at 9:48 PM Peter Langfelder <[hidden email]>
wrote:

> oops, I think the right code would be
>
> x = get(varname)
> attr(x, "foo") = "bar"
> assign(varname, x)
>
> On Wed, Oct 10, 2018 at 9:30 PM Peter Langfelder <
> [hidden email]>
> wrote:
>
> > I would try something like
> >
> > x = get(myvarname)
> > attr(x, "foo") = "bar"
> > assign(varname, x)
> >
> > HTH,
> >
> > Peter
> >
> > On Wed, Oct 10, 2018 at 9:15 PM Marc Girondot via R-help <
> > [hidden email]> wrote:
> >
> >> 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...
> >>
> >> ______________________________________________
> >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >>
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.