[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
assign(paste(i,"$V4",sep=""),paste(get(i),"$V2+",get(i),"$V3",sep=""))
To me, the former is much simpler, much easier to read and understand.
There are several problems with
i<-24
assign(paste(i,"$V4",sep=""),paste(get(i),"$V2+",get(i),"$V3",sep=""))
First, I think you must have meant
i <- letters[24]
You seem to expect that
paste( get(i) , "$V2+" )
would result in
"x$V2+"
but it doesn't.
The expression
get(i)
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+"
With
paste(get(i),"$V2+",get(i),"$V3",sep="")
you appear to be trying to construct
"x$V2+x$V3"
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
search()
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
Try:
eval(parse(text='3+4'))
or
> eval(parse(text= paste('3','+','4')))
[1] 7
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
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:
>
>
>x=as.data.frame(matrix(c(1,2,3,
> 1,2,3,
> 1,2,2,
> 1,2,2,
> 1,1,1),ncol=3,byrow=T))
>
>y=as.data.frame(matrix(c(1,2,3,
> 1,2,3,
> 1,2,2,
> 1,2,2,
> 1,1,1),ncol=3,byrow=T))
>
>z=as.data.frame(matrix(c(1,2,3,
> 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)) )
>x
>y
>z
>
>And I figured how to do further processing using functions:
>
>myfunc<-function(DF){
> 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
>
>x$V4<-x$V2+x$V3
>y$V4<-y$V2+y$V3
>z$V4<-z$V2+z$V3
>
>is there a simpler way to do this than building a function as shown above?
> I tried a few variations of
>
>i<-24
>assign(paste(i,"$V4",sep=""),paste(get(i),"$V2+",get(i),"$V3",sep=""))
>
>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
>it.
>
>--John Sparks
>
>______________________________________________
>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.
More information about the R-help
mailing list