R加入后保留数据框属性 [英] R keep data frame attributes after join
问题描述
我想保留一些联接后的数据框属性。似乎 dplyr
函数保存列中的属性,但不保存数据框本身中的属性。
I would like to keep data frame attributes after some joins. It seems that dplyr
functions holds attributes from columns but not the ones from the data frame itself.
请参见示例
library("dplyr")
library("lubridate")
#Fake data
n = 20
df <- data.frame("user_id" = 1:n,
"type" = sample(c(1,2), n, replace = T),
"amount" = 1000*rnorm(n))
#Suppose I want to add some metadata
attr(df, "query_timestamp") <- lubridate::now()
attr(df$amount, "currency") <- "BRL"
#encoding table for user type
encode <- data.frame("type" = c(1,2),
"description" = c("vendor", "shopper"))
print(attr(df, "query_timestamp"))
[1] 2018-07-18 15 :30:57 -03
[1] "2018-07-18 15:30:57 -03"
print(attr(df$amount, "currency"))
[1] BRL
[1] "BRL"
df <- df %>% dplyr::left_join(encode, by = "type")
print(attr(df, "query_timestamp"))
NULL
NULL
print(attr(df$amount, "currency"))
[1] BRL
[1] "BRL"
有什么理由吗?我想保留属性,但避免使用aux变量存储它们。
Is there any reason for that? I would like to keep attributes but avoid using aux variables to store them.
推荐答案
您可以使用<重新附加属性code> attr <-函数:
df <- df %>% dplyr::left_join(encode, by = "type") %>%
`attr<-`("query_timestamp", attr(df,"query_timestamp") )
> print(attr(df, "query_timestamp"))
[1] "2018-07-18 14:41:39 PDT"
通常,调用将是以下项之一:
Normally the call would be one of:
`attr(df, "query_timestamp") <- attr(df,"query_timestamp") )
# or equivalently
`attr<-`(df, "query_timestamp", attr(df,"query_timestamp") )
但是您可能知道,如果第一个参数是正在处理的对象,则可以将其删除。这样,您可以在执行破坏性分配(<-
)之前重新附加。因此,您需要将联接之前的属性另存为一个单独的值,然后在联接之后的一个单独步骤中重新附加它,或者以这种方式(在破坏性的后退分配之前重新分配。)
But as you probably know the first argument can be dropped if it is the object that is being processed. This way you reattach before the destructive assignment (<-
) is executed. So you need to either save the attribute before the join as a separate value and then reattach it in a separate step after the join, or do it this way (reassigning just before the destructive "back"-assignment.
这篇关于R加入后保留数据框属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!