I've the following data.table
structure(list(xi = c(1, 1, 1, 2, 2, 2, 3, 3, 3), yi = c(1, 2,
3, 1, 2, 3, 1, 2, 3), flag = c(0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("xi",
"yi", "flag"), row.names = c(NA, -9L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x11a1a78>)
I also have a 3x3
matrix as below.
structure(c(1, 1, 0.4, 1, 0, 0, 1, 0, 0.2), .Dim = c(3L, 3L))
I want to assign a third column to the data.table flag
such that if the element in the matrix represented by the xi
row and yi
column is less than 1, then flag = 1
else 0. I wrote a function for this,
func <- function (x, y, m) {
if (m[x, y] < 1) {
return(1)
}
else {
return(0)
}
}
However, if I try
y[,flag := func(xi,yi,m)]
my flag values are always 0. Could someone point out what I'm doing wrong here? Thanks in advance.