0
votes

I need to do a for loop and a true or false operation for each cell value in the table, if operation returns true, then the function is going to replace the value with 0. But the R throws me errors everytime i try to run the code

for (i in xxxxx(a number){
   for (j in xxx(a number){
     if (y[i,j] == 0 & y[i,j] < mean(y) - ppp / sd(y)){
       y[i,j] <- NA
     }
   }
}

Error in [.data.table(y, i, j) : j (the 2nd argument inside [...]) is a single symbol but column name 'j' is not found. Perhaps you intended DT[, ..j]. This difference to data.frame is deliberate and explained in FAQ 1.1.

1
What are the xxxxx and the xxx functions? Further, R won't accept a number as an input so this is probably mot the code that you run. It is easier to answer if your example is reproducible and you show the expected output. - machine

1 Answers

0
votes

I wonder how you're supposed to do mean(y) and sd(y), as y seems to be a data table based on your code.

Your mean(y) would have thrown an error, but your y[i,j] errored first, most likely it's because your i or j doesn't make sense.

If your y is all numbers, you can use matrix instead of data.table. Vectorizing the solution is always faster in R.

y <- as.matrix(y);

y[some condition for y] <- NA

If vectorizing is not an option, the way you loop through cells is also slow. When you use [i, j], it takes time for R to find the row. You can take a look at my test.