如何在使用 left_join() 合并数据时保留标签?

huangapple go评论68阅读模式
英文:

How do I keep the labels when merging data with left_join()?

问题

我有两个手动添加标签的数据框。我想要合并这两个数据框,同时保留来自两个数据框的标签。这篇帖子建议使用left_join()可以实现这个目标。

我尝试了以下方法:

library(dplyr)

# 创建第一个示例数据框
df1 = data.frame(matrix(1:10, nrow=5))
attributes(df1)$variable.labels[1] <- "Reference Variable"
attributes(df1)$variable.labels[2] <- "Label 1"

# 创建第二个示例数据框
df2 = data.frame(matrix(1:30, nrow=10))
names(df2)[2] <- "Y2"
names(df2)[3] <- "Y3"
attributes(df2)$variable.labels[2] <- "Label 2"
attributes(df2)$variable.labels[3] <- "Label 3"

# 合并两个数据框
merged_data <- left_join(x = df1, y = df2, by = "X1")

# df1的标签仍然存在,而来自df2的标签不在合并的数据中
attributes(merged_data)$variable.labels[2]
attributes(merged_data)$variable.labels[3]
attributes(merged_data)$variable.labels[4]

在合并后的数据框中,Y2和Y3的标签丢失了。期望的结果是将数据框merged_data中包含来自df1和df2的所有标签。

是否有办法实现这个目标?

英文:

I have two dataframes with manually added labels. I want to merge these two dataframes while preserving labels from both dataframes. This post suggets that left_join() does precisely that.

I tried the following approach:

library(dplyr)

# Create first sample dataframe
df1 = data.frame(matrix(1:10, nrow=5))
attributes(df1)$variable.labels[1] &lt;- &quot;Reference Variable&quot;
attributes(df1)$variable.labels[2] &lt;- &quot;Label 1&quot;

# Create second sample dataframe
df2 = data.frame(matrix(1:30, nrow=10))
names(df2)[2] &lt;- &quot;Y2&quot;
names(df2)[3] &lt;- &quot;Y3&quot;
attributes(df2)$variable.labels[2] &lt;- &quot;Label 2&quot;
attributes(df2)$variable.labels[3] &lt;- &quot;Label 3&quot;

# Merge both dataframes
merged_data &lt;- left_join(x = df1, y = df2, by = &quot;X1&quot;)

# Labels of df1 still exist while the ones from df2 don&#39;t
attributes(merged_data)$variable.labels[2]
attributes(merged_data)$variable.labels[3]
attributes(merged_data)$variable.labels[4]

In the merged dataframe the labels for Y2 and Y3 are missing. The desired outcome is to have the dataframe merged_data with all the labels from df1 and df2.

Is there a way to achieve that?

答案1

得分: 0

一种解决方法是使用attr()重新附加属性。例如:

attr(merged_data, "variable.labels") <- c(
  attr(df1, "variable.labels"),
  attr(df2, "variable.labels")
)

在管道内部进行操作可以像这样完成:

merged_data <-
  left_join(x = df1, y = df2, by = "X1") %>%
  `attr<-`(
    "variable.labels",
    c(attr(df1, "variable.labels"),
      attr(df2, "variable.labels"))
  )
英文:

One solution would be to reattach the attributes using attr(). For instance:

attr(merged_data, &quot;variable.labels&quot;) &lt;- c(
  attr(df1, &quot;variable.labels&quot;),
  attr(df2, &quot;variable.labels&quot;)
)

Doing it inline a pipe could be done like this:

merged_data &lt;-
  left_join(x = df1, y = df2, by = &quot;X1&quot;) %&gt;%
  `attr&lt;-`(
    &quot;variable.labels&quot;,
    c(attr(df1, &quot;variable.labels&quot;),
      attr(df2, &quot;variable.labels&quot;))
  )

huangapple
  • 本文由 发表于 2023年3月7日 20:50:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定