Skip to content

Evaluating an expression

3 messages · Janszen, Derek, David Winsemius, Bert Gunter

#
Hi,

I want to create a data frame similar to the following, but greatly scaled up:
df <- data.frame(aaa= c("a","b","c"), integer(3), integer(3))
names(df)[2:3] <- paste("var",1:2,sep="")
which yields
  aaa  var1  var2
1   a     0     0
2   b     0     0
3   c     0     0

I would not relish having to paste 'integer(3)' 5000 times :(

So (I figure) there must be a way to do this programmatically, something akin to
exp1 <- paste(rep("integer(3)",2),collapse=',')
which looks like it might work:     "integer(3),integer(3)" , as in the following
df <- data.frame(aaa=xxx, eval(parse(text=exp1)))
but this yields

Error in parse(text = exp1) : <text>:1:11: unexpected ','

1: integer(3),

              ^


Not sure just why this doesn't work (?parse does not help), but it's not important right now.

I have used eval and parse in the past, but not in a way similar to what I'm trying to do now.



exp1 <- rep("integer(3)",2) gives "integer(3)" "integer(3)"

and upon parse(text=exp1) gives expression(integer(3), integer(3))

which appears to be promising, and does not give an error in the following

        df <- data.frame(aaa=xxx, eval(parse(text=exp1)))

but alas, does not give the desired result
  aaa eval.parse.text...exp1..
1   a                        0
2   b                        0
3   c                        0




I'm guessing that only the last evaluation of the expression is being evaluated, which I can understand.

I feel certain that what I want to accomplish can be done programmatically, but am at a loss as to just how to do that.
Chances are this has been covered before. If so, apologies.
If not, can anyone point me to references with more info than the help pages, or suggest a solution? :)

Thanks,
Derek

Derek Janszen, PhD
Statistician, Analytics

Precision for Medicine<http://www.precisionformedicine.com/>
8425 Progress Drive, Suite M | Frederick, MD 21701
+1 240 415 6004 office



The information contained in this email and any attachments is confidential and may be subject to copyright or other intellectual property protection. If you are not the intended recipient, you are not authorized to use or disclose this information, and we request that you notify us by reply mail or telephone and delete the original message from your mail system.
#
V1 V2 V3 V4 V5
1  1  1  1  1  1
2  2  2  2  2  2
3  3  3  3  3  3

Or:
a x x.1 x.2 x.3 x.4
1 a 0   0   0   0   0
2 b 0   0   0   0   0
3 c 0   0   0   0   0


Just replace the 3's and 5's with a number (possibly delivered via a named numeric vector of length-1) of your choosing.
#
I think you need to spend some time with an R tutorial or two to learn
how to handle such basics yourself.

However, if I understand correctly, probably the easiest way to do it
is by converting a matrix of 0's to a data frame -- which you
shouldn't do at all if you can do your analysis directly with the
matrix (it's almost always faster, sometimes considerably so). e.g.

adf <- data.frame(matrix(0, nrow=10,ncol=20))

Of course, you may then wish to provide more informative names for
your columns via the names() function.

Also, I would question whether you need to do any of this in the first
place. It is rarely necessary in R to initialize a data frame or
matrix.

Finally, you should almost never need the eval(parse()) construction,
which treats R as a macro language instead of taking advantage of its
functional programming paradigm. Again, the of sort thing that good R
tutorials can help you with. There are many on the web. Some good
recommendations can be found here:

https://www.rstudio.com/online-learning/#R

HTH,

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 Mon, Apr 4, 2016 at 9:22 AM, Janszen, Derek
<derek.janszen at precisionformedicine.com> wrote: