Dear R users,
I would like to change the class of a group of objects in R memory from
"numeric" to "dist". I can manipulate the class using
class(foo) <- bar
but I cannot get the same command to work on groups of variables. When I
use for() loops and lists of names, inevitably I have to specify
class(get("foo")) <- bar
which causes class() to return an error. Could anyone offer some advice
how to manipulate classes and attributes of a list of objects in memory?
Many thanks,
Ben
a <- c(1:5)
b <- c(1:10)
c <- c(1:20)
# What I would like to do is coerce all of these to class "dist".
# How can I write a command that changes the class of a, b, AND c to
"dist"?
# Most of my answers include a list or a for() loop over (a, b, c)
# How can I write a command that changes attributes of a, b, and c all
at the same time?
# I can force the process to work for a few objects, but it is
time-consuming.
# For reference, I am including the following commands.
class(a)
class(get(letters[1]))
class(a) <- "dist"
class(a) <- NULL
class(a)
class(get(letters[1])) <- "dist"
--
Ben Mazzotta
PhD Candidate
Fletcher School, Tufts University
160 Packard Ave, Medford MA 02155
benjamin.mazzotta at tufts.edu
+1.617.462.4486
Ben, In general, when you define a, b, and c as below, the c() wrapper is not needed, since the : operator returns a vector anyway. Also, best not to name variables 'c' to avoid confusion with the function you're using. Regarding your actual question, lapply(list(a, b, c), "class<-", "dist") might work, but your objects in your example don't seem to be able to be coerced to dist objects. E.g., as.dist(b) gives an error. If you have data that can actually work as dist objects, you might try the above line though. Erik> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Ben Mazzotta > Sent: Monday, November 23, 2009 4:51 PM > To: r-help at r-project.org > Subject: [R] how to change the class of a group of objects > > Dear R users, > > I would like to change the class of a group of objects in R memory from > "numeric" to "dist". I can manipulate the class using > > class(foo) <- bar > > but I cannot get the same command to work on groups of variables. When I > use for() loops and lists of names, inevitably I have to specify > > class(get("foo")) <- bar > > which causes class() to return an error. Could anyone offer some advice > how to manipulate classes and attributes of a list of objects in memory? > > Many thanks, > Ben > > > > > a <- c(1:5) > b <- c(1:10) > c <- c(1:20) > > # What I would like to do is coerce all of these to class "dist". > # How can I write a command that changes the class of a, b, AND c to > "dist"? > # Most of my answers include a list or a for() loop over (a, b, c) > # How can I write a command that changes attributes of a, b, and c all > at the same time? > # I can force the process to work for a few objects, but it is > time-consuming. > # For reference, I am including the following commands. > > class(a) > class(get(letters[1])) > class(a) <- "dist" > class(a) <- NULL > class(a) > class(get(letters[1])) <- "dist" > > > -- > Ben Mazzotta > PhD Candidate > Fletcher School, Tufts University > 160 Packard Ave, Medford MA 02155 > benjamin.mazzotta at tufts.edu > +1.617.462.4486 > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Henrique Dallazuanna
2009-Nov-23 23:26 UTC
[R] how to change the class of a group of objects
Try this:
assign("a", `class<-`(get("a"), "character"))
On Mon, Nov 23, 2009 at 8:51 PM, Ben Mazzotta
<benjamin.mazzotta at tufts.edu> wrote:> Dear R users,
>
> I would like to change the class of a group of objects in R memory from
> "numeric" to "dist". I can manipulate the class using
>
> class(foo) <- bar
>
> but I cannot get the same command to work on groups of variables. When I
> use for() loops and lists of names, inevitably I have to specify
>
> class(get("foo")) <- bar
>
> which causes class() to return an error. Could anyone offer some advice
> how to manipulate classes and attributes of a list of objects in memory?
>
> Many thanks,
> Ben
>
>
>
>
> a <- c(1:5)
> b <- c(1:10)
> c <- c(1:20)
>
> # What I would like to do is coerce all of these to class "dist".
> # How can I write a command that changes the class of a, b, AND c to
> "dist"?
> # Most of my answers include a list or a for() loop over (a, b, c)
> # How can I write a command that changes attributes of a, b, and c all
> at the same time?
> # I can force the process to work for a few objects, but it is
> time-consuming.
> # For reference, I am including the following commands.
>
> class(a)
> class(get(letters[1]))
> class(a) <- "dist"
> class(a) <- NULL
> class(a)
> class(get(letters[1])) <- "dist"
>
>
> --
> Ben Mazzotta
> PhD Candidate
> Fletcher School, Tufts University
> 160 Packard Ave, Medford MA 02155
> benjamin.mazzotta at tufts.edu
> +1.617.462.4486
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paran?-Brasil
25? 25' 40" S 49? 16' 22" O