英文:
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] <- "Reference Variable"
attributes(df1)$variable.labels[2] <- "Label 1"
# Create second sample dataframe
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"
# Merge both dataframes
merged_data <- left_join(x = df1, y = df2, by = "X1")
# Labels of df1 still exist while the ones from df2 don'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, "variable.labels") <- c(
attr(df1, "variable.labels"),
attr(df2, "variable.labels")
)
Doing it inline a pipe could be done like this:
merged_data <-
left_join(x = df1, y = df2, by = "X1") %>%
`attr<-`(
"variable.labels",
c(attr(df1, "variable.labels"),
attr(df2, "variable.labels"))
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论