1

Newbie R question:

I'd like to use the ceiling function to round up the y variable in each of ten training datasets, train1 through train10.

> for (i in 1:10){ x <- get(paste0("train",i)); x$y <- ceiling(x$y) }

The above code runs without error. However when I check the y values of my datasets, I discover they haven't been rounded:

> head(train1$y)
[1] 29561.06     0.00     0.00  4660.24   440.00   924.60

But if I try this:

> head(x$y)
[1] 29562     0     0  4661   772   440
> head(x$Fold)
[1] 10 10 10 10 10 10

it turns out the code is working correctly, but only on the renamed dataset "x" which gets overwritten after each loop, not datasets train1-train10.

What am I doing wrong?

RobertF
  • 824
  • 2
  • 14
  • 40

2 Answers2

2

It would be better to keep the datasets in a 'list'. But if you want to update the data objects in the global environment, one option is list2env. We get the 'datasets' in a list with mget, loop through the 'list' with lapply and create a new variable 'y', update the data objects with list2env.

list2env(lapply(mget(paste0('train', 1:10)), function(x) {
           x$y <- ceiling(x$y)
            x}), envir=.GlobalEnv)

Or using assign

for(i in 1:10) {
     assign(paste0('train', i), `[[<-`(get(paste0('train', i)), 'y', 
               value= ceiling(get(paste0('train', i))$y)),
       envir=.GlobalEnv)}
head(train1,3)
#  y
#1 0
#2 1
#3 1

data

set.seed(24)
list2env(setNames(lapply(1:10, function(i) 
    data.frame(y= rnorm(5))), paste0('train', 1:10)), envir=.GlobalEnv)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

or ... but itis very very very bad

for (i in 1:2) {
 tmp =data.frame(x=get(paste0("train",i))$x,y=ceiling(get(paste0("train",i))$y));
 assign(paste0("train",i), tmp)
} 
kwicher
  • 2,092
  • 1
  • 19
  • 28
  • This works too, thanks kwicher. So I gather using for-loops is bad form - do loops use more memory or time to execute than the lapply function? – RobertF Jul 15 '15 at 19:58
  • I am the begginer too and I asked almost exactly the samequestion a couple of days ago, please see the discussion there: http://stackoverflow.com/questions/31386135/passing-arguments-to-a-function-by-their-names-in-r – kwicher Jul 15 '15 at 20:19