Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

R: access element in a column of lists with data.table

$
0
0

I'm using data.table to store a big array of objects, and I want to access one of those objects, but it always returns the result wrapped in a list.

library(data.table)

a1 = hist(1:10)
a2 = hist(2:11)
a3 = hist(3:12)
a4 = hist(4:13)
a5 = hist(5:14)
a6 = hist(6:15)

a = list(a1,a2,a3,a4,a5,a6)
i = c(1,1,1,2,2,2)
j = c(1,2,3,1,2,3)

dt = data.table(i = i, j = j, a = a)
class(dt[i == 1 & j == 1, a]) # hist inside a list of length 1

I'd like the previous line to return to object itself (just the histogram), like this:

class(a1) # just the hist

Obviously I could do:

dt[i == 1 & j == 1, a][[1]]

But it seems inelegant to do that every time. Any way to achieve this using syntax within data.table? Note: there is another question with a very similar name, but it's asking something more complicated.


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>