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?
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With