On 29/07/11 11:09, Sarah Henderson wrote:
Greetings to all --

I am having a silly problem that I just can't solve.  Someone has given me
an .RData file will hundreds of data frames.  Each data frame has a column
named ResultValue, currently character when it should be numeric.  I want to
loop through all of the data frames to change the variable to numeric, but I
cannot figure out how to do it. My best guess was along the lines of:

frames = ls()
for (frame in frames){
      assign(frame, get(frame), .GlobalEnv)
      frame[,"ResultValue"] = as.numeric(frame[,"ResultValue"])
      }


Note that ``frame'' is an object the value of which is a character string
naming (successive) data frames. Your call to assign doesn't change the
value of ``frame''. It *could* (but doesn't actually) change the value of the
object *named* by the character string stored in ``frame''.

Suppose that you have data frames clyde, melvin, and irving. On your first pass
through the loop the value of frame is the character string ``clyde''.

Your assign() call in effect does ``clyde <- clyde'' which doesn't accomplish
much. :-)

The value of frame after the assign() is still the character string ``clyde''.
I.e. frame is a character scalar, so frame[,"ResultValue"] doesn't make any
sense.

What you need to do is something like:

    frames <- ls()
    for(frame in frames) {
assign("temp",get(frame)) # Don't think you need the ``.GlobalEnv'', although it doesn't hurt.
        temp[,ResultValue"] <- as.numeric(temp[,"ResultValue"])
        assign(frame,temp)
    }

This is untested ....

But I hope it helps.

        cheers,

            Rolf Turner

______________________________________________
R-help@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.

Reply via email to