3

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?

2 Answers 2

2

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)
 }
Sign up to request clarification or add additional context in comments.

Comments

0

This may have a slight performance penalty for not subsetting first before the update, but I find it more readable:

dataT[, colnames(dataT) := lapply(.SD, pmax, .5)][]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.