Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycling and assignment functions (`split<-`)

Can someone please explain me how this one line of R code works?

split(dat, f) <- lapply(split(dat, f), max)

I thought it is a just a recycling rule but really I can't understand it.

Data example :

dat <- c(1, 2, 3, 100, 200, 300)
f <- as.factor(c("a", "a", "b", "a", "b", "b"))
split(dat, f) <- lapply(split(dat, f), max)
dat
[1] 100 100 300 100 300 300

The code do what I want to do (assign the max by group) but the question is how this is done?

like image 210
agstudy Avatar asked Jan 12 '13 14:01

agstudy


2 Answers

The split gives the values dat[c(1,2,4)] and dat[c(3,5,6)] from the vector.

The assignment is equivalent to dat[c(1,2,4)] <- 100 ; dat[c(3,5,6)] <- 300 and this is where the recycling takes place.

Edited

As for what happens, and why a vector assignment results, see page 21 of the language definition manual (http://cran.r-project.org/doc/manuals/R-lang.pdf). The call:

split(def, f) <- Z

Is interpreted as:

‘*tmp*‘ <- def
def <- "split<-"(‘*tmp*‘, f, value=Z)
rm(‘*tmp*‘)

Note that split<-.default returns the modified vector.

like image 129
Matthew Lundberg Avatar answered Sep 23 '22 02:09

Matthew Lundberg


Thanks to the comment , the answer is in split<-.default

Just to explain its behavior, here I call the split<-.default with result of my call in the question

`split<-.default` <- function(dat, f,value = lapply(split(dat, f), max))
{
    ix <- split(seq_along(dat), f, drop = drop, ...)  ## the call of split here!!
    n <- length(value)
    j <- 0
    for (i in ix) {
        j <- j %% n + 1
        x[i] <- value[[j]]  ## here we assign the result of the first split
    }
    x
}
like image 43
agstudy Avatar answered Sep 22 '22 02:09

agstudy