Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Data Frame Columns in R mmap objects

Tags:

r

I am trying to port some code to use the mmap package. I am having an issue with accessing data frame columns.

I would like to be able to access data columns with the $ and [[ operators. Here is the results I am getting.

> foo <- as.mmap(mtcars)
> foo[,'mpg'] # works
    mpg
1  21.0
2  21.0
3  22.8
4  21.4
5  18.7
 ...
> foo$mpg #does not work
NULL
> foo[['mpg']] #also does not work
NULL
> foo[]$mpg #works
...
> foo[][['mpg']] #also works
...

Is there any way to make the $ and [[ operators work on the memory mapped object, as they would on a regular data frame?

Edit: Per Joshua's suggestion I added a function for [[

`[[.mmap` <- function(x,...) `[[`(x[],...)

And for $ which does not seem particularly elegant, but seems to work.

> `$.mmap` <- function(x,...) {
  if (...%in%c("storage.mode","bytes","extractFUN","filedesc")){
    get(...,envir=x) 
  }else {
    eval(call('$',x[],substitute(...)))
  }}
like image 850
aaronjg Avatar asked Jan 05 '12 18:01

aaronjg


1 Answers

Those functions don't work because they don't have a mmap method.

> grep("mmap",methods("["),value=TRUE)
[1] "[.mmap"
> grep("mmap",methods("[["),value=TRUE)
character(0)
> grep("mmap",methods("$"),value=TRUE)
character(0)

Therefore, they dispatch to the default methods, which have no idea how to handle a mmap object. You would need to write mmap methods for [[ and $.

like image 170
Joshua Ulrich Avatar answered Nov 12 '22 18:11

Joshua Ulrich