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?
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With