`levels<-`( 这是什么魔法? [英] `levels<-`( What sorcery is this?
问题描述
在回答另一个问题时,@Marek 发布了以下解决方案:https://stackoverflow.com/a/10432263/636656
In an answer to another question, @Marek posted the following solution: https://stackoverflow.com/a/10432263/636656
dat <- structure(list(product = c(11L, 11L, 9L, 9L, 6L, 1L, 11L, 5L,
7L, 11L, 5L, 11L, 4L, 3L, 10L, 7L, 10L, 5L, 9L, 8L)), .Names = "product", row.names = c(NA, -20L), class = "data.frame")
`levels<-`(
factor(dat$product),
list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)
)
产生的输出:
[1] Generic Generic Bayer Bayer Advil Tylenol Generic Advil Bayer Generic Advil Generic Advil Tylenol
[15] Generic Bayer Generic Advil Bayer Bayer
这只是一个向量的打印输出,所以为了存储它你可以做更令人困惑的事情:
This is just the printout of a vector, so to store it you can do the even more confusing:
res <- `levels<-`(
factor(dat$product),
list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)
)
显然这是对 levels 函数的某种调用,但我不知道这里做了什么.这种巫术的术语是什么,我如何提高我在这个领域的魔法能力?
Clearly this is some kind of call to the levels function, but I have no idea what's being done here. What is the term for this kind of sorcery, and how do I increase my magical ability in this domain?
推荐答案
这里的答案很好,但缺少一个重要的点.让我试着描述一下.
The answers here are good, but they are missing an important point. Let me try and describe it.
R 是一种函数式语言,不喜欢改变它的对象.但它确实允许赋值语句,使用替换函数:
R is a functional language and does not like to mutate its objects. But it does allow assignment statements, using replacement functions:
levels(x) <- y
相当于
x <- `levels<-`(x, y)
诀窍是,这种重写是由<-
完成的;它不是由 levels<-
完成的.levels<-
只是一个接受输入并给出输出的常规函数;它不会改变任何东西.
The trick is, this rewriting is done by <-
; it is not done by levels<-
. levels<-
is just a regular function that takes an input and gives an output; it does not mutate anything.
这样做的一个后果是,根据上述规则,<-
必须是递归的:
One consequence of that is that, according to the above rule, <-
must be recursive:
levels(factor(x)) <- y
是
factor(x) <- `levels<-`(factor(x), y)
是
x <- `factor<-`(x, `levels<-`(factor(x), y))
这种纯函数转换(直到最后,赋值发生的地方)与命令式语言中的赋值等效,这有点美妙.如果我没记错的话,函数式语言中的这种结构称为镜头.
It's kind of beautiful that this pure-functional transformation (up until the very end, where the assignment happens) is equivalent to what an assignment would be in an imperative language. If I remember correctly this construct in functional languages is called a lens.
但是,一旦你定义了像 levels<-
这样的替换函数,你就会得到另一个意想不到的意外收获:你不仅有能力进行分配,你还有一个方便的函数在一个因子中,并给出不同水平的另一个因子.真的没有什么任务"!
But then, once you have defined replacement functions like levels<-
, you get another, unexpected windfall: you don't just have the ability to make assignments, you have a handy function that takes in a factor, and gives out another factor with different levels. There's really nothing "assignment" about it!
因此,您所描述的代码只是利用了对 levels<-
的另一种解释.我承认 levels<-
这个名字有点令人困惑,因为它暗示了一个分配,但这不是正在发生的事情.代码只是设置了一种管道:
So, the code you're describing is just making use of this other interpretation of levels<-
. I admit that the name levels<-
is a little confusing because it suggests an assignment, but this is not what is going on. The code is simply setting up a sort of pipeline:
从
dat$product
将其转换为因子
更改级别
将其存储在 res
就个人而言,我认为这行代码很漂亮;)
Personally, I think that line of code is beautiful ;)
这篇关于`levels<-`( 这是什么魔法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!