回收和分配功能(`split< -`) [英] Recycling and assignment functions (`split<-`)
问题描述
split(dat,f)< - > 有人可以请我解释一下这一行R代码的工作原理吗? lapply(split(dat,f),max)
我认为这只是一个回收规则,但
数据示例:
(c(a,a,b,a,b), ,b))
split(dat,f)< - lapply(split(dat,f),max)
dat
[1] 100 100 300 100 300 300
代码做我想做的事(按组分配最大值),但问题在于如何完成?
分割给出值 赋值相当于 已编辑 b 至于发生了什么以及向量赋值结果的原因,请参见语言定义手册的第21页(http://cran.r-project。组织/ DOC /手册/ R-lang.pdf)。通话: 解释为: b $ b Can someone please explain me how this one line of R code works? I thought it is a just a recycling rule but really I can't understand it. Data example : 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 The assignment is equivalent to 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: Is interpreted as: Note that 这篇关于回收和分配功能(`split< -`)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! dat [c(1,2,4)]
和
dat [c(3,5,6)]
来自向量。
dat [c(1,2,4)] < - 100; dat [c(3,5,6)] < - 300
,这就是回收的地方。
$ b
split(def,f)< - Z
'* tmp *'< ; - def
def< - split< - ('* tmp *',f,value = Z)
rm('* tmp *')
$ c $注意, split < - 。default
返回修改过的向量。split(dat, f) <- lapply(split(dat, f), max)
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
dat[c(1,2,4)]
and dat[c(3,5,6)]
from the vector.dat[c(1,2,4)] <- 100 ; dat[c(3,5,6)] <- 300
and this is where the recycling takes place.split(def, f) <- Z
‘*tmp*‘ <- def
def <- "split<-"(‘*tmp*‘, f, value=Z)
rm(‘*tmp*‘)
split<-.default
returns the modified vector.