Kazusa12345
Kazusa12345

Reputation: 111

data.table error in r related to referencing certain objects

I have the following example

a <- c("a","b","c","d","e","f")
b <- 1:6
c <- c("a","E","D","C","B","A")
d <- 10:15

dt1 <- data.table("ID" = b,"code_a" = a,"code_c" = c, "code_d" = d)
dt2 <- data.table("ID" = b, "code_c" = c, "code_d" = d )
dt3 <- data.table("ID" = b, "code_d" = d)
merged_list <- list(dt1,dt2,dt3)
output <- merged_list[[1]][merged_list[[1]][,2] != merged_list[[1]][,3]]

The Error test reads as follows:

Error in [.data.table(merged_list[[1]], merged_list[[1]][, 2] != merged_list[[1]][, : i is invalid type (matrix). Perhaps in future a 2 column matrix could return a list of elements of DT (in the spirit of A[B] in FAQ 2.14). Please report to data.table issue tracker if you'd like this, or add your comments to FR #657.

Here I want to get the output for first item in merged_list, which is a data.table, but given condition where the second item in the data.table must not equal to the third item in the data.table

I am unsure why the above doesn't work, but if i use $code_a and $code_c instead of [,2] and [,3] it works. I would not like to use $ referencing because it does not work nicely in a loop.

Expected Output:

output
#     ID code_a code_c code_d
# 1:  2      b      E     11
# 2:  3      c      D     12
# 3:  4      d      C     13
# 4:  5      e      B     14
# 5:  6      f      A     15

As only the first row has code_a equals to code_c so that is removed. Thanks for the help. My main objective here is to not use $code_a as i cant put it in a for loop which gives me code_a, code_b, code_c etc. etc. Any help into what I did wrong and how I can solve this issue is greatly appreciated. And sorry for the non informative title, I am not exactly sure what this kind of problem is.

Upvotes: 2

Views: 401

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389325

Yes, because as the error message suggest the comparison returns a matrix.

merged_list[[1]][,2] != merged_list[[1]][,3]
#     code_a
#[1,]  FALSE
#[2,]   TRUE
#[3,]   TRUE
#[4,]   TRUE
#[5,]   TRUE
#[6,]   TRUE

class(merged_list[[1]][,2] != merged_list[[1]][,3])
#[1] "matrix"

Turn it into a vector and it should work

merged_list[[1]][c(merged_list[[1]][,2] != merged_list[[1]][,3])]

#   ID code_a code_c code_d
#1:  2      b      E     11
#2:  3      c      D     12
#3:  4      d      C     13
#4:  5      e      B     14
#5:  6      f      A     15

Upvotes: 3

Related Questions