Juan Manuel Barreneche
2007-Aug-21 19:35 UTC
[R] how do i use the get function to obtain an element from a list...
my problem can be explained with the following example:
x <- 1:12
y <- 13:24
a <- data.frame(x = x, y = y)
## if i write
a$x
## it returns
[1] 1 2 3 4 5 6 7 8 9 10 11 12
## but the function get doesn't recognize a$x. Instead it produces the
following error:
get("a$x")
Error in get(x, envir, mode, inherits) : variable "a$x" was not found
i intend to do it inside a loop, using a new object (and hence, a new
name) for each iteration (i.e., instead of a$x, it would be a$1, a$2,
a$3, and so on, for a million times).
i would greatly appreciate it if someone could help me on this issue,
thanks in advance,
Juan Manuel Barreneche,
Zoolog?a de Vertebrados,
Facultad de Ciencias,
UDELAR, Uruguay.
Leeds, Mark (IED)
2007-Aug-21 19:39 UTC
[R] how do i use the get function to obtain an element from alist...
eval(parse(text=("a$x"). You can only use get when it's an object.
a$x isn't.
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at
stat.math.ethz.ch] On Behalf Of Juan Manuel Barreneche
Sent: Tuesday, August 21, 2007 3:35 PM
To: r-help at stat.math.ethz.ch
Subject: [R] how do i use the get function to obtain an element from alist...
my problem can be explained with the following example:
x <- 1:12
y <- 13:24
a <- data.frame(x = x, y = y)
## if i write
a$x
## it returns
[1] 1 2 3 4 5 6 7 8 9 10 11 12
## but the function get doesn't recognize a$x. Instead it produces the
following error:
get("a$x")
Error in get(x, envir, mode, inherits) : variable "a$x" was not found
i intend to do it inside a loop, using a new object (and hence, a new
name) for each iteration (i.e., instead of a$x, it would be a$1, a$2, a$3, and
so on, for a million times).
i would greatly appreciate it if someone could help me on this issue,
thanks in advance,
Juan Manuel Barreneche,
Zoolog?a de Vertebrados,
Facultad de Ciencias,
UDELAR, Uruguay.
______________________________________________
R-help at stat.math.ethz.ch 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.
--------------------------------------------------------
This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
Duncan Murdoch
2007-Aug-21 19:44 UTC
[R] how do i use the get function to obtain an element from a list...
On 8/21/2007 3:35 PM, Juan Manuel Barreneche wrote:> my problem can be explained with the following example: > > x <- 1:12 > y <- 13:24 > a <- data.frame(x = x, y = y) > > ## if i write > a$x > ## it returns > [1] 1 2 3 4 5 6 7 8 9 10 11 12 > > ## but the function get doesn't recognize a$x. Instead it produces the > following error: > get("a$x") > Error in get(x, envir, mode, inherits) : variable "a$x" was not founda$x is an expression, which you could evaluate, not a variable, which you could get.> > i intend to do it inside a loop, using a new object (and hence, a new > name) for each iteration (i.e., instead of a$x, it would be a$1, a$2, > a$3, and so on, for a million times). > > i would greatly appreciate it if someone could help me on this issue,Why name things? I'd use something like for (i in 1:1000000) print(a[[i]]) Duncan Murdoch
Henrique Dallazuanna
2007-Aug-21 19:45 UTC
[R] how do i use the get function to obtain an element from a list...
Hi, you can try this: eval(parse(text="a$x")) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 21/08/07, Juan Manuel Barreneche <jumanbar@gmail.com> wrote:> > my problem can be explained with the following example: > > x <- 1:12 > y <- 13:24 > a <- data.frame(x = x, y = y) > > ## if i write > a$x > ## it returns > [1] 1 2 3 4 5 6 7 8 9 10 11 12 > > ## but the function get doesn't recognize a$x. Instead it produces the > following error: > get("a$x") > Error in get(x, envir, mode, inherits) : variable "a$x" was not found > > i intend to do it inside a loop, using a new object (and hence, a new > name) for each iteration (i.e., instead of a$x, it would be a$1, a$2, > a$3, and so on, for a million times). > > i would greatly appreciate it if someone could help me on this issue, > > thanks in advance, > > Juan Manuel Barreneche, > Zoología de Vertebrados, > Facultad de Ciencias, > UDELAR, Uruguay. > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
Tony Plate
2007-Aug-21 20:38 UTC
[R] how do i use the get function to obtain an element from a list...
One simple way that I haven't seen mentioned yet is to do:
> get("a")$x
(which of course allows further variants such as get("a")$x[3:6] ...)
-- Tony Plate
Juan Manuel Barreneche wrote:> my problem can be explained with the following example:
>
> x <- 1:12
> y <- 13:24
> a <- data.frame(x = x, y = y)
>
> ## if i write
> a$x
> ## it returns
> [1] 1 2 3 4 5 6 7 8 9 10 11 12
>
> ## but the function get doesn't recognize a$x. Instead it produces the
> following error:
> get("a$x")
> Error in get(x, envir, mode, inherits) : variable "a$x" was not
found
>
> i intend to do it inside a loop, using a new object (and hence, a new
> name) for each iteration (i.e., instead of a$x, it would be a$1, a$2,
> a$3, and so on, for a million times).
>
> i would greatly appreciate it if someone could help me on this issue,
>
> thanks in advance,
>
> Juan Manuel Barreneche,
> Zoolog?a de Vertebrados,
> Facultad de Ciencias,
> UDELAR, Uruguay.
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>