r - data.table filter list column for empty values -


can filter list-column in data.table rows empty lists?

library(data.table) dt = data.table(a = c(1, 2, 3), b = list(c("a", "b"), character(0), c("c", "d", "e")))  > dt     b 1: 1   1,2 2: 2       3: 3 1,2,3 

i.e. expected result

> dt[filter(b)]    b 1: 2   

the obvious filtering doesn't work

> dt[length(b) == 0] empty data.table (0 rows) of 2 cols: a,b  > dt[length(b[[1]]) == 0] empty data.table (0 rows) of 2 cols: a,b 

i thought might able define function result in right boolean value, have use group make work, doesn't work in filter argument

is_null_list = function(l) is.list(l) & length(l[[1]]) == 0  > dt[, is_null_list(b), a]    v1 1: 1 false 2: 2  true 3: 3 false  > dt[is_null_list(b)] empty data.table (0 rows) of 2 cols: a,b 

i guess more general question also, can filtering done on data.table list columns? suspect answer no can't key list, thought worth asking.

thanks

you can filter length of each element of list column lengths. example,

dt[ lengths(b) == 0l ] 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -