-3

I have a big data.table df consisting of Integer values between 0 and 1 and I'd like to change some of the columns. These are named as shown below. I would like to do this for 10 scaling factors, also integer, so I thought a loop would be more efficient. I used the package data.table.

Cur_1 Cur_2 Cur_3 Cur_4 Cur_5 Cur_6 Cur_7 Cur_8 Cur_9 Cur_10 Cur_11 Cur_12

0.225 0.227 0.229 0.233 0.225 0.226 0.228 0.229 0.229 0.301 0.302 0.305

0.226 0.227 0.229 0.233 0.225 0.226 0.228 0.229 0.229 0.301 0.302 0.305

if(pd_scaling1!=0){
df$Cur_1=df$Cur_1*(1+scaling1)
df$Cur_2=df$Cur_2*(1+scaling1)
df$Cur_3=df$Cur_3*(1+scaling1)
df$Cur_4=df$Cur_4*(1+scaling1)
df$Cur_5=df$Cur_5*(1+scaling1)
df$Cur_6=df$Cur_6*(1+scaling1)
df$Cur_7=df$Cur_7*(1+scaling1)
df$Cur_8=df$Cur_8*(1+scaling1)
df$Cur_9=df$Cur_9*(1+scaling1)
df$Cur_10=df$Cur_10*(1+scaling1)
df$Cur_11=df$Cur_11*(1+scaling1)
df$Cur_12=df$Cur_12*(1+scaling1)
}

I tried this:

for(i in 1:10){
  if(get(paste("scaling",i,sep=""))!=0){
    for(j in (i-1)*12+1:i*12){
         df[,get(paste("Cur",j,sep="_")):=get(paste("Cur",j,sep="_"))*rep((1+get(paste("scaling",i,sep=""))),h_i)
    }
  }

But got the following error:

Error in get(paste("Cur", j, sep = "_")) : object 'Cur_2' not found }

Mathfreak
  • 1
  • 2
  • 3
    Please see [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and fix your question in order to make it possible for us to help you. – David Arenburg Jun 08 '18 at 09:39
  • Please read the data.table vignettes. They show how to properly loop over columns. – Roland Jun 08 '18 at 10:28
  • @Roland could you please send me a link, I couln't find it in [data.table vignettes](https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html) – Mathfreak Jun 08 '18 at 10:49
  • See 2.e in that vignette and 2.e in [this one](https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reference-semantics.html). – Roland Jun 08 '18 at 10:52
  • @abhiieor thanks for the tip, but it doesn't work – Mathfreak Jun 08 '18 at 11:07

1 Answers1

0

This is the first thing that came to my mind (and maybe not the optimal solution) of applying the scale value to the columns in the data.table given a condition.

First I created a data.table based on your data:

library(data.table)
names <- sapply(1:12,function(x) paste0("Cur_",x))
d1 <- c(0.225, 0.227, 0.229, 0.233, 0.225, 0.226, 0.228, 0.229, 0.229, 0.301, 0.302, 0.305)
d2 <- c(0.226, 0.227, 0.229, 0.233, 0.225, 0.226, 0.228, 0.229, 0.229, 0.301, 0.302, 0.305)
D <- sapply(1:12, function(x) list(d1[x], d2[x]))
DD <- data.table(D)
colnames(DD) <- names
head(DD)

The data.table look like this:

   Cur_1 Cur_2 Cur_3 Cur_4 Cur_5 Cur_6 Cur_7 Cur_8 Cur_9 Cur_10 Cur_11 Cur_12
1: 0.225 0.227 0.229 0.233 0.225 0.226 0.228 0.229 0.229  0.301  0.302  0.305
2: 0.226 0.227 0.229 0.233 0.225 0.226 0.228 0.229 0.229  0.301  0.302  0.305

I suppose that you are trying to apply a scale value to all the columns, so I create a scaling variable with a random number of the same length of the columns in the data.table.

scaling <- rep(100,length(names))

I have see that you are trying to verify if the scale value is different to 0, so I intentionally put one of the scale values to 0, just for show the behaviour of the solution I made.

scaling[2] <- 0

Now we create a loop that verify the value of the scaling and apply the scale to the columns of the data.table

for (i in 1:length(names)) {
    if (scaling[i] != 0) {
        DD[,names[i] := lapply(.SD, function(x) unlist(x)*scaling[i]), .SDcols = names[i]]
    }
}

The final data.table looks like this:

head(DD)
 Cur_1 Cur_2 Cur_3 Cur_4 Cur_5 Cur_6 Cur_7 Cur_8 Cur_9 Cur_10 Cur_11 Cur_12
1:  22.5 0.227  22.9  23.3  22.5  22.6  22.8  22.9  22.9   30.1   30.2   30.5
2:  22.6 0.227  22.9  23.3  22.5  22.6  22.8  22.9  22.9   30.1   30.2   30.5

Some tips: I highly suggest you to read the source provided by David Arenburg, because I had to guess what were the things you wanted. The source of Roland in the comments is very useful and explains well the solution to your problem.

Random Cotija
  • 613
  • 1
  • 10
  • 26