Kumar Manglam Kumar Manglam - 1 month ago 6
R Question

Updating column values in data.table, based on column values

Consider the below code :

library(data.table)
dataT <-data.table(1:15,runif(15),runif(15),runif(15))

for(vrb in names(dataT)) {
dataT[get(vrb) < 0.5, (vrb):=0.5] # update value
}


As can be inferred from the code, I am basically capping the lowest value of each column to 0.5. To subset rows, I have used
get
function.

Is this correct way of doing, or is there any other way, which is more aligned with data.table?

Answer

We can use set

for(vrb in names(dataT)){
 set(dataT, i = which(dataT[[vrb]] < 0.5), j = vrb, value = 0.5)
}

The elements in the first column is > 0.5. So, we can apply the set on columns except the first

for(vrb in names(dataT)[-1]){
  set(dataT, i = which(dataT[[vrb]] < 0.5), j = vrb, value = 0.5)
 }
Comments