Hi all, Suppose I want to set the values in a column to the log of the values of another column like so: object$LogDist <- log10(object$Distance) How do I loop through the objects if I have object1, object2, etc to perform this function? object1$LogDist <- log10(object1$Distance) object2$LogDist <- log10(object2$Distance) object3$LogDist <- log10(object3$Distance) I was trying to use the assign and paste functions like so: for (i in c(1:10)) assign(paste("object",i,"$LogDist"),log10(as.name(paste("object",i,"$LogDist"))) but that didn't work out. It creates objects with whole name object1$LogDist instead of just manipulating the column that's part of that object. Help appreciated, Vivek
I would first put the objects in a 'list' since it is easier to work with and then use 'lapply':> obj1 <- obj2 <- obj3 <- data.frame(Distance=1:10) > # create a list - earier to work with > list.obj <- list(obj1, obj2, obj3) > > list.obj <- lapply(list.obj, function(x){+ x$LogDist <- log(x$Distance) + x # return the modified value + })> > list.obj[[1]] Distance LogDist 1 1 0.0000000 2 2 0.6931472 3 3 1.0986123 4 4 1.3862944 5 5 1.6094379 6 6 1.7917595 7 7 1.9459101 8 8 2.0794415 9 9 2.1972246 10 10 2.3025851 [[2]] Distance LogDist 1 1 0.0000000 2 2 0.6931472 3 3 1.0986123 4 4 1.3862944 5 5 1.6094379 6 6 1.7917595 7 7 1.9459101 8 8 2.0794415 9 9 2.1972246 10 10 2.3025851 [[3]] Distance LogDist 1 1 0.0000000 2 2 0.6931472 3 3 1.0986123 4 4 1.3862944 5 5 1.6094379 6 6 1.7917595 7 7 1.9459101 8 8 2.0794415 9 9 2.1972246 10 10 2.3025851 On Thu, Jul 30, 2009 at 12:04 PM, Vivek Ayer<vivek.ayer at gmail.com> wrote:> Hi all, > > Suppose I want to set the values in a column to the log of the values > of another column like so: > > object$LogDist <- log10(object$Distance) > > How do I loop through the objects if I have object1, object2, etc to > perform this function? > > object1$LogDist <- log10(object1$Distance) > object2$LogDist <- log10(object2$Distance) > object3$LogDist <- log10(object3$Distance) > > I was trying to use the assign and paste functions like so: > > for (i in c(1:10)) > assign(paste("object",i,"$LogDist"),log10(as.name(paste("object",i,"$LogDist"))) > > but that didn't work out. It creates objects with whole name > object1$LogDist instead of just manipulating the column that's part of > that object. > > Help appreciated, > Vivek > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Got that work! Thanks. However, now I need to know how to dump tag values into an object, e.g., TotLogDist <- c(object1$LogDist,object2$LogDist,object3$LogDist ...) [for how many ever objects you provided] Is there a way to loop that? TotLogDist <- NULL for (i in c(1:10)) TotLogDist <- c(TotLogDist,objecti$LogDist) After this, I don't need to do anymore loops etc. I promise I'll use lists, lapply, and sapply from then on. Thanks, Vivek On Thu, Jul 30, 2009 at 9:04 AM, Vivek Ayer<vivek.ayer at gmail.com> wrote:> Hi all, > > Suppose I want to set the values in a column to the log of the values > of another column like so: > > object$LogDist <- log10(object$Distance) > > How do I loop through the objects if I have object1, object2, etc to > perform this function? > > object1$LogDist <- log10(object1$Distance) > object2$LogDist <- log10(object2$Distance) > object3$LogDist <- log10(object3$Distance) > > I was trying to use the assign and paste functions like so: > > for (i in c(1:10)) > assign(paste("object",i,"$LogDist"),log10(as.name(paste("object",i,"$LogDist"))) > > but that didn't work out. It creates objects with whole name > object1$LogDist instead of just manipulating the column that's part of > that object. > > Help appreciated, > Vivek >
I figured it out. I understand that R isn't really meant to do this. Essentially, I want to have an object per read csv file which contains columns. But I'm dealing with hundreds of files in a folder and I wanted to automate the importing into R and process stuff without entering the shell and using lists, lapply, and sapply. I realized there's an more convenient indexing symbol for these kind of functions, namely "[[ , ]]" So I did something like this: for (i in c(1:num)) { tempobj <- get(paste(id,i,sep="")); TotLogDist <- c(TotLogDist,tempobj[["LogDist"]]) } where num is now ~100 or 1000. Thanks for the tips! I'm still a newbie in R, and hopefully I can learn to be more efficient next time. Thanks again, Vivek On Thu, Jul 30, 2009 at 3:01 PM, Phil Spector<spector at stat.berkeley.edu> wrote:> Vivek - > ? In R, you shouldn't be storing related objects in > individual vectors or data frames, you should put them in > a list. ?Frankly, I don't know what you're trying to > acheive with the code fragment you sent, so I don't really > know what to tell you, but it's usually not a good idea in R to have to > paste together strings to access a variable. > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? - Phil > > > On Thu, 30 Jul 2009, Vivek Ayer wrote: > >> How I loop object placement into a list? >> >> for (i in c(1:10)) >> list(paste("object",i,sep="")) >> ?? >> >> Kind of confused. This would be useful for functions like c() as well. >> >> Thanks, >> Vivek >> >> btw...I stop by 5th floor Evans every once in a while! >> >> On Thu, Jul 30, 2009 at 9:33 AM, Phil Spector<spector at stat.berkeley.edu> >> wrote: >>> >>> Vivek - >>> ? First, put your objects in a list where they belong: >>> >>> myobjects = list(object1,object2,object3) >>> >>> Now you can use the power of R to do your work: >>> >>> myobjects = lapply(myobjects,transform,logDist = log10(Distance)) >>> >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? - Phil Spector >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Statistical Computing Facility >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Department of Statistics >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? UC Berkeley >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? spector at stat.berkeley.edu >>> >>> P.S. - ?If you really want to do things with loops, get, assign, etc. >>> you could do something like this: >>> >>> for(i in 1:10){ >>> ? ?objname = paste('object',i,sep='') >>> ? ?now = get(objname) >>> ? ?assign(objname,transform(now,logDist = log10(distance))) >>> } >>> >>> But please try to remember that R provides lists and functions like >>> lapply and sapply for a reason. >>> >>> On Thu, 30 Jul 2009, Vivek Ayer wrote: >>> >>>> Hi all, >>>> >>>> Suppose I want to set the values in a column to the log of the values >>>> of another column like so: >>>> >>>> object$LogDist <- log10(object$Distance) >>>> >>>> How do I loop through the objects if I have object1, object2, etc to >>>> perform this function? >>>> >>>> object1$LogDist <- log10(object1$Distance) >>>> object2$LogDist <- log10(object2$Distance) >>>> object3$LogDist <- log10(object3$Distance) >>>> >>>> I was trying to use the assign and paste functions like so: >>>> >>>> for (i in c(1:10)) >>>> >>>> >>>> assign(paste("object",i,"$LogDist"),log10(as.name(paste("object",i,"$LogDist"))) >>>> >>>> but that didn't work out. It creates objects with whole name >>>> object1$LogDist instead of just manipulating the column that's part of >>>> that object. >>>> >>>> Help appreciated, >>>> Vivek >>>> >>>> ______________________________________________ >>>> 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. >>>> >>> >