R 回收和分配功能(`split<;-`)
有人能给我解释一下这一行R代码是怎么工作的吗R 回收和分配功能(`split<;-`),r,syntax,functional-programming,R,Syntax,Functional Programming,有人能给我解释一下这一行R代码是怎么工作的吗 split(dat, f) <- lapply(split(dat, f), max) split(dat,f)分割给出向量中的值dat[c(1,2,4)]和dat[c(3,5,6)] 这个作业相当于dat[c(1,2,4)]多亏了这个评论,答案就在splitLook`split另一个可爱的卡布姆时刻,来自R手册谢谢!我喜欢这个答案。Myabe你需要解释为什么我们得到的是向量而不是列表。 dat <- c(1, 2, 3, 100, 2
split(dat, f) <- lapply(split(dat, f), max)
split(dat,f)分割给出向量中的值dat[c(1,2,4)]
和dat[c(3,5,6)]
这个作业相当于dat[c(1,2,4)]多亏了这个评论,答案就在splitLook`split另一个可爱的卡布姆时刻,来自R手册谢谢!我喜欢这个答案。Myabe你需要解释为什么我们得到的是向量而不是列表。
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
split(def, f) <- Z
‘*tmp*‘ <- def
def <- "split<-"(‘*tmp*‘, f, value=Z)
rm(‘*tmp*‘)
`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
}