So I have a loop that I want to implement which assigns the original value (in row a and col b) + 1 if the value (at row a and col b) is equal to a certain number, however, when i try to assign the number using with = FALSE
, I am unable to do so, and removing it yields me an output where the whole col is =
to original value (in row a and col b) + 1.
Test Case:
a <- rep(0,6)
b <- 1:6
c <- -3:2
d <- runif(6)
e <-runif(6)
dt1 <- data.table("ID" = b,"code_a" = a,"code_c" = c, "code_d" = d, "code_e" = e)
dt1
ID code_a code_c code_d code_e
1: 1 0 -3 0.5369538 0.269854945
2: 2 0 -2 0.7186787 0.009384648
3: 3 0 -1 0.8053726 0.831286263
4: 4 0 0 0.2084106 0.171294349
5: 5 0 1 0.0130021 0.730679582
6: 6 0 2 0.2902858 0.062175009
So in the following I have assigned ctr <- 1
and tested the scenario that I wanted to replace col_2
and row_1
with the number 1, but unfortunately, neither scenario is my desired output.
My Attempt number 1:
dt1[,eval(ctr+1), with = FALSE][ctr] <- 1
Error in `[<-.data.table`(`*tmp*`, , eval(ctr + 1), with = FALSE, value = list( :
unused argument (with = FALSE)
My Attempt number 2:
dt1[,eval(ctr+1)][ctr] <- 1
> dt1
ID code_a code_c code_d code_e
1: 1 1 -3 0.5369538 0.269854945
2: 2 1 -2 0.7186787 0.009384648
3: 3 1 -1 0.8053726 0.831286263
4: 4 1 0 0.2084106 0.171294349
5: 5 1 1 0.0130021 0.730679582
6: 6 1 2 0.2902858 0.062175009
Desired Output:
ID code_a code_c code_d code_e
1: 1 1 -3 0.5369538 0.269854945
2: 2 0 -2 0.7186787 0.009384648
3: 3 0 -1 0.8053726 0.831286263
4: 4 0 0 0.2084106 0.171294349
5: 5 0 1 0.0130021 0.730679582
6: 6 0 2 0.2902858 0.062175009
This is probably asked before, but I am unable to find the correct way to find the thread that asked this question. I also tried using ..ctr
but to no success. If anyone can help me understand whats going on, it would be greatly appreciated.