0
votes

I am trying to use the data.table package in order to try to save memory on my data frame (which is 1.8 gig), but I'm having some difficulties with reading just 1 element. when using a data.frame, I would read the element by using the first call, and I would get the value as shown in the second call, without the need for the as.numeric() conversion. Since data.table inherits data.frame, I was expecting the same to be the case. Am I missing something?

DT = data.table(x=letters[1:3], y=1:9)
> DT[1,2]
   y
1: 1
> as.numeric(DT[1,2])
[1] 1
3
I did check those out, but I did not see how to read a single item. It appears the FAQ has the answer though.Xizam

3 Answers

4
votes

Your solution of using as.numeric() works ... as long as the data is in fact numeric, or can be coerced.

But you can use the fact that data.table cleanly subclasses data.frame:

R> DT <- data.table(x=letters[1:3], y=1:9)
R> DT[1,2]
   y
1: 1
R> unlist(DT[1,2]) ## DF is a list of vectors, so start with `unlist)_`
y 
1 
R> unname(unlist(DT[1,2]))  ## an `unname()`
[1] 1
R> 

This should work equivalently for non-numeric or non-integer data.

1
votes

Data.table does not like it when you refer to a column by column number and wants you to refer to it by name. The easiest way is to do as such:

DT = data.table(x=letters[1:3], y=1:9)
> DT[1,y]
[1] 1
1
votes

It seemed that data.table faq have been updated. The very first answer is about this:

1.1 Why do DT[ , 5] and DT[2, 5] return a 1-column data.table rather than vectors like data.frame?

For the original question, I think it's easier just use this

> DT[1,2]
   y
1: 1
> DT[1,2][[1]]
[1] 1