I'm sure there is a more general way to ask this question but how do
you use the elements of a character vector as names of objects in an
expression?
For example, say you have:
a = c(1,3,5,7)
b = c(2,4,6,8)
n=c("a","b")
and you want to use the names a and b in a function (e.g. sum)
sum(eval(as.name(n[1])),eval(as.name(n[2])))
works but
what is a simpler way to effect this level of indirection?
eval and as.name
8 messages · Fuchs Ira, Murray Cooper, Wacek Kusnierczyk +2 more
I am new to R, so maybe I'm missing the point of your question. But why wouldn't you just use sum(a,b)? Murray M Cooper, Ph.D. Richland Statistics 9800 N 24th St Richland, MI, USA 49083 Mail: richstat at earthlink.net ----- Original Message ----- From: "Fuchs Ira" <irafuchs at gmail.com> To: <r-help at r-project.org> Sent: Thursday, February 05, 2009 5:10 PM Subject: [R] eval and as.name
I'm sure there is a more general way to ask this question but how do you
use the elements of a character vector as names of objects in an
expression?
For example, say you have:
a = c(1,3,5,7)
b = c(2,4,6,8)
n=c("a","b")
and you want to use the names a and b in a function (e.g. sum)
sum(eval(as.name(n[1])),eval(as.name(n[2])))
works but
what is a simpler way to effect this level of indirection?
______________________________________________ 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.
Fuchs Ira wrote:
I'm sure there is a more general way to ask this question but how do
you use the elements of a character vector as names of objects in an
expression?
For example, say you have:
a = c(1,3,5,7)
b = c(2,4,6,8)
n=c("a","b")
and you want to use the names a and b in a function (e.g. sum)
sum(eval(as.name(n[1])),eval(as.name(n[2])))
works but
what is a simpler way to effect this level of indirection?
in this particular case, the simplest i can come up with is: sum(sapply(n, get)) you may want to avoid this sort of indirection by using lists with named components: d = list(a=c(1,3,5,7), b=c(2,4,6,8)) sum(unlist(d)) with(d, sum(a+b)) sum(d[['a']], d[['b']]) sum(sapply(n, function(v) d[[v]])) and so on. vQ
Murray Cooper wrote:
I am new to R, so maybe I'm missing the point of your question. But why wouldn't you just use sum(a,b)?
if you know you want to sum a and b, sure you would. if you need to sum
the variables named by the components of some dynamic character vector,
you need to lookup ('dereference') the names in some way.
vQ
Murray M Cooper, Ph.D. Richland Statistics 9800 N 24th St Richland, MI, USA 49083 Mail: richstat at earthlink.net ----- Original Message ----- From: "Fuchs Ira" <irafuchs at gmail.com> To: <r-help at r-project.org> Sent: Thursday, February 05, 2009 5:10 PM Subject: [R] eval and as.name
I'm sure there is a more general way to ask this question but how do
you use the elements of a character vector as names of objects in an
expression?
For example, say you have:
a = c(1,3,5,7)
b = c(2,4,6,8)
n=c("a","b")
and you want to use the names a and b in a function (e.g. sum)
sum(eval(as.name(n[1])),eval(as.name(n[2])))
works but
what is a simpler way to effect this level of indirection?
______________________________________________ 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.
______________________________________________ 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.
Wacek Kusnierczyk wrote:
Fuchs Ira wrote:
I'm sure there is a more general way to ask this question but how do
you use the elements of a character vector as names of objects in an
expression?
For example, say you have:
a = c(1,3,5,7)
b = c(2,4,6,8)
n=c("a","b")
and you want to use the names a and b in a function (e.g. sum)
sum(eval(as.name(n[1])),eval(as.name(n[2])))
works but
what is a simpler way to effect this level of indirection?
in this particular case, the simplest i can come up with is: sum(sapply(n, get)) you may want to avoid this sort of indirection by using lists with named components: d = list(a=c(1,3,5,7), b=c(2,4,6,8)) sum(unlist(d)) with(d, sum(a+b))
should have been one of with(d, sum(a,b)) with(d, a+b) vQ
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090206/dd9cc0bb/attachment-0001.pl>
Marie Sivertsen wrote:
Hi, Why do you use the equals sign for assignment instead of the arrow, is this equal?
equal? you mean equivalent? mostly, yes. briefly, this is why: 1. a copy-over from other programming languages; 2. to avoid learning yet another operator; 3. after having learned the other operator, to avoid that ugly operator; 4. after an r guru complained here about people using this instead of that, to annoy him. hilsen, vQ
Mvh. Marie On Thu, Feb 5, 2009 at 11:59 PM, Wacek Kusnierczyk < Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
you may want to avoid this sort of indirection by using lists with named components: d = list(a=c(1,3,5,7), b=c(2,4,6,8)) sum(unlist(d)) with(d, sum(a+b)) sum(d[['a']], d[['b']]) sum(sapply(n, function(v) d[[v]])) and so on. vQ
______________________________________________ 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.
Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk <at> idi.ntnu.no> writes:
equal? you mean equivalent? mostly, yes. briefly, this is why: 1. a copy-over from other programming languages; 2. to avoid learning yet another operator; 3. after having learned the other operator, to avoid that ugly operator; 4. after an r guru complained here about people using this instead of that, to annoy him.
:-) It's always fun to read.... Dieter