[R] Looping through R objects with $ operator and tags

Vivek Ayer vivek.ayer at gmail.com
Thu Jul 30 23:39:39 CEST 2009


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
>




More information about the R-help mailing list