[R] Adding Column to Data Frames Using a Loop

MacQueen, Don macqueen1 at llnl.gov
Thu May 2 17:58:45 CEST 2013

I'm a little puzzled, because you're asking for something simpler than
  for(i in letters[24:26] ) assign( i, myfunc(get(i)))
and suggesting things along the lines of
To me, the former is much simpler, much easier to read and understand.

There are several problems with


First, I think you must have meant

  i <- letters[24]

You seem to expect that
  paste( get(i) , "$V2+" )
would result in
but it doesn't.

The expression
returns the entire object, in this case a data frame, in particular,
the data frame named 'x'. Look at what happens when you try to
paste a data frame with something else

> paste(x,'$V2+')
[1] "c(1, 1, 1, 1, 1) $V2+" "c(2, 2, 2, 2, 1) $V2+" "c(3, 3, 2, 2, 1) $V2+"

same as

> paste( get('x') , '$V2+')
[1] "c(1, 1, 1, 1, 1) $V2+" "c(2, 2, 2, 2, 1) $V2+" "c(3, 3, 2, 2, 1) $V2+"


you appear to be trying to construct

and then hoping that expression will be executed. And then hoping that the
results will get assigned within the data frame to x$V4.

But assign() writes into an environment, not into a data frame. For now,
think of environments as one of the elements that you see when you run

It is possible to construct the expression you want executed, and cause it
to be executed, but it's kind of an advanced approach and I think more
trouble than it's worth. In 15 or so years of using R, I've used it maybe
once or twice. I really think you're better off with lists, and would
second Bill Dunlap's request for more explanation of why you don't want to
go that route.

If you really want to construct R expressions and cause them to be
evaluated, I'd start by looking at


> eval(parse(text= paste('3','+','4')))
[1] 7

Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

On 5/1/13 9:53 AM, "Sparks, John James" <jspark4 at uic.edu> wrote:

>Dear R Helpers,
>I am trying to do calculations on multiple data frames and do not want to
>create a list of them to go through each one.  I know that lists have many
>wonderful advantages, but I believe the better thing is to work df by df
>for my particular situation.  For background, I have already received some
>wonderful help on how to handle some situations, such as removing columns:
>        1,2,3,
>        1,2,2,
>        1,2,2,
>       1,1,1),ncol=3,byrow=T))
>        1,2,3,
>        1,2,2,
>        1,2,2,
>       1,1,1),ncol=3,byrow=T))
>        1,2,3,
>        1,2,2,
>        1,2,2,
>       1,1,1),ncol=3,byrow=T))
>for(i in letters[24:26] ) assign( i, subset(get(i), select=-c(V1))  )
>And I figured how to do further processing using functions:
> DF$V4<-DF$V2+DF$V3
> return(DF)
>for(i in letters[24:26] ) assign( i, myfunc(get(i)))
>But if I want to do a rather simple calculation and store it as a new
>column in each data frame such as
>is there a simpler way to do this than building a function as shown above?
> I tried a few variations of
>but keep getting syntax errors.
>If anyone could help with the syntax as to how to accomplish the
>calculation above without building a function, I would really appreciate
>--John Sparks
>R-help at r-project.org mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list