Tuesday 16 December 2014

A Christmas R parcel: refset

I have a new R package on github, called refset. refset's tagline is "subsets with reference semantics". In English, that means: a refset is a subset which automatically stays in sync with its parent data. When you change the parent, the refset is updated, and when you change the refset, you are changing the parent.

The README has full details, but here's a brief taster:

Installation

library(devtools)
install_github("hughjonesd/refset")

Creating a refset

library(refset)
employees <- data.frame( 
      id=1:4,
      name=c("James", "Sylvia", "Meng Qi", "Luis"), 
      age=c(28,44,38, 23), 
      gender=factor(c("M", "F", "F", "M")), 
      stringsAsFactors=FALSE)
      
refset(rs, employees[1:2,])

Refsets refer to the original

rs 
##   id   name age gender
## 1  1  James  28      M
## 2  2 Sylvia  44      F 
employees$name[1] <- "Jimmy"
rs 
##   id   name age gender
## 1  1  Jimmy  28      M
## 2  2 Sylvia  44      F

Refsets change the original

rs$age <- c(29, 45)
employees$age
## [1] 29 45 38 23
... continued on github. Enjoy!

No comments:

Post a Comment