Sarah Henderson
2011-Jul-28 23:09 UTC
[R] Looping through data sets to change column from character to numeric
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"]) } It doesn't work. After the assign() the frame object remains the character name of the dataframe I am trying to change. If I do the following, the TEST object comes out just fine. frames = ls() for (frame in frames){ assign("TEST", get(frame), .GlobalEnv) TEST[,"ResultValue"] = as.numeric(TEST[,"ResultValue"]) } Seems like it should be simple, but I am misunderstanding something and not following the logic. Any insight? Thanks, Sarah [[alternative HTML version deleted]]
"Dénes TÓTH"
2011-Jul-28 23:36 UTC
[R] Looping through data sets to change column from character to numeric
The problem is that you can not assign a variable to itself. rm(list=ls()) df1 <- data.frame(ResultValue=as.character(1:5)) df2 <- data.frame(ResultValue=as.character(1:10)) frames = ls() for (frame in frames){ temp <- get(frame) temp[,"ResultValue"] = as.numeric(temp[,"ResultValue"]) assign(frame,temp) } HTH, Denes> 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"]) > } > > It doesn't work. After the assign() the frame object remains the > character > name of the dataframe I am trying to change. If I do the following, the > TEST object comes out just fine. > > frames = ls() > for (frame in frames){ > assign("TEST", get(frame), .GlobalEnv) > TEST[,"ResultValue"] = as.numeric(TEST[,"ResultValue"]) > } > > Seems like it should be simple, but I am misunderstanding something and > not > following the logic. Any insight? > > Thanks, > > Sarah > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Rolf Turner
2011-Jul-29 05:34 UTC
[R] Looping through data sets to change column from character to numeric
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
Greg Snow
2011-Jul-31 02:22 UTC
[R] Looping through data sets to change column from character to numeric
Others have shown how to do this, but a better approach may be to load your .Rdata file into a new environment rather than the global environment, then you can work with the environment (and all the objects in it) using lapply, or loops with [[]] instead of fighting with get and assign. Something like:> mydfs <- new.env() > load( '.Rdata', env=mydfs > mydfs <- lapply( mydfs, function(x) { x$ResultValue <- as.numeric(x$ResultValue); x} )Or> mydfs <- lapply( mydfs, function(x) within(x, ResultValue <- as.numeric(ResultValue) ) )Or> for( frame in ls(env=mydfs) ) {+ mydfs[[frame]]$ResultValue <- as.numeric( mydfs[[frame]]$ResultValue ) + } -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Sarah Henderson Sent: Thursday, July 28, 2011 5:09 PM To: R List Subject: [R] Looping through data sets to change column from character to numeric 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"]) } It doesn't work. After the assign() the frame object remains the character name of the dataframe I am trying to change. If I do the following, the TEST object comes out just fine. frames = ls() for (frame in frames){ assign("TEST", get(frame), .GlobalEnv) TEST[,"ResultValue"] = as.numeric(TEST[,"ResultValue"]) } Seems like it should be simple, but I am misunderstanding something and not following the logic. Any insight? Thanks, Sarah [[alternative HTML version deleted]] ______________________________________________ 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.
Possibly Parallel Threads
- problem with package development and older defs earlier in search order
- how to save an R object selectively?
- Model object, when generated in a function, saves entire environment when saved
- Model object, when generated in a function, saves entire environment when saved
- using tcltk in R under ESS/XEmacs on Windows