回收和分配功能(`split< -`) [英] Recycling and assignment functions (`split<-`)

查看:144
本文介绍了回收和分配功能(`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

代码做我想做的事(按组分配最大值),但问题在于如何完成?

解决方案

分割给出值 dat [c(1,2,4)] dat [c(3,5,6)] 来自向量。

赋值相当于 dat [c(1,2,4)] < - 100; dat [c(3,5,6)] < - 300 ,这就是回收的地方。

已编辑 b
$ b

至于发生了什么以及向量赋值结果的原因,请参见语言定义手册的第21页(http://cran.r-project。组织/ DOC /手册/ R-lang.pdf)。通话:

  split(def,f)< -  Z 

解释为:

 '* tmp *'< ;  -  def 
def< - split< - ('* tmp *',f,value = Z)
rm('* tmp *')
split < - 。default
返回修改过的向量。

$

b $ b

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.

这篇关于回收和分配功能(`split&lt; -`)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆