在dplyr包中以链接方式添加属性 [英] Adding attributes in chaining way in dplyr package
本文介绍了在dplyr包中以链接方式添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用dplyr
包中的链接序列代码运算符%>%
添加属性?
Is there any way to add an attribute using chaining sequence code operator %>%
from dplyr
package?
> library(dplyr)
> iris %>%
+ attr( "date") = Sys.Date()
Error in iris %>% attr("date") = Sys.Date() :
could not find function "%>%<-"
>
感谢您的回复.
推荐答案
您也可以考虑从"data.table"中获取setattr
:
You can also consider setattr
from "data.table":
library(dplyr)
library(data.table)
names(attributes(iris))
# [1] "names" "row.names" "class"
iris %>% setattr(., "date", Sys.Date())
names(attributes(iris))
# [1] "names" "row.names" "class" "date"
attributes(I2)$date
# [1] "2014-09-04"
当然,像这样的东西实际上不需要链接.您可以这样做:
Of course, no chaining is actually required for something like this. You could just do:
setattr(iris, "date", Sys.Date())
这使您可以设置属性而无需复制有问题的对象:
This allows you to set attributes without copying the objects in question:
> v1 <- 1:4
> v2 <- 1:4
> tracemem(v1)
[1] "<0x0000000011cffa38>"
> attr(v1, "foo") <- "bar"
tracemem[0x0000000011cffa38 -> 0x0000000011d740f8]:
> tracemem(v2)
[1] "<0x0000000011db2da0>"
> setattr(v2, "foo", "bar")
> attributes(v2)
$foo
[1] "bar"
这篇关于在dplyr包中以链接方式添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文