Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

data.table

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?

like image 910
Kumar Manglam Avatar asked Apr 20 '26 15:04

Kumar Manglam


1 Answers

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)
 }
like image 190
akrun Avatar answered Apr 23 '26 00:04

akrun