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

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

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

问题

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

我尝试了以下方法:

  1. library(dplyr)
  2. # 创建第一个示例数据框
  3. df1 = data.frame(matrix(1:10, nrow=5))
  4. attributes(df1)$variable.labels[1] <- "Reference Variable"
  5. attributes(df1)$variable.labels[2] <- "Label 1"
  6. # 创建第二个示例数据框
  7. df2 = data.frame(matrix(1:30, nrow=10))
  8. names(df2)[2] <- "Y2"
  9. names(df2)[3] <- "Y3"
  10. attributes(df2)$variable.labels[2] <- "Label 2"
  11. attributes(df2)$variable.labels[3] <- "Label 3"
  12. # 合并两个数据框
  13. merged_data <- left_join(x = df1, y = df2, by = "X1")
  14. # df1的标签仍然存在,而来自df2的标签不在合并的数据中
  15. attributes(merged_data)$variable.labels[2]
  16. attributes(merged_data)$variable.labels[3]
  17. 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:

  1. library(dplyr)
  2. # Create first sample dataframe
  3. df1 = data.frame(matrix(1:10, nrow=5))
  4. attributes(df1)$variable.labels[1] &lt;- &quot;Reference Variable&quot;
  5. attributes(df1)$variable.labels[2] &lt;- &quot;Label 1&quot;
  6. # Create second sample dataframe
  7. df2 = data.frame(matrix(1:30, nrow=10))
  8. names(df2)[2] &lt;- &quot;Y2&quot;
  9. names(df2)[3] &lt;- &quot;Y3&quot;
  10. attributes(df2)$variable.labels[2] &lt;- &quot;Label 2&quot;
  11. attributes(df2)$variable.labels[3] &lt;- &quot;Label 3&quot;
  12. # Merge both dataframes
  13. merged_data &lt;- left_join(x = df1, y = df2, by = &quot;X1&quot;)
  14. # Labels of df1 still exist while the ones from df2 don&#39;t
  15. attributes(merged_data)$variable.labels[2]
  16. attributes(merged_data)$variable.labels[3]
  17. 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()重新附加属性。例如:

  1. attr(merged_data, "variable.labels") <- c(
  2. attr(df1, "variable.labels"),
  3. attr(df2, "variable.labels")
  4. )

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

  1. merged_data <-
  2. left_join(x = df1, y = df2, by = "X1") %>%
  3. `attr<-`(
  4. "variable.labels",
  5. c(attr(df1, "variable.labels"),
  6. attr(df2, "variable.labels"))
  7. )
英文:

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

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

Doing it inline a pipe could be done like this:

  1. merged_data &lt;-
  2. left_join(x = df1, y = df2, by = &quot;X1&quot;) %&gt;%
  3. `attr&lt;-`(
  4. &quot;variable.labels&quot;,
  5. c(attr(df1, &quot;variable.labels&quot;),
  6. attr(df2, &quot;variable.labels&quot;))
  7. )

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:

确定