本文介绍了在R中合并和保留变量标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并以大量变量为特征的不同数据框,这些变量用诸如 c302 , c303 等。从SPSS导入的原始文件保留了非常有用的标签。

I´m trying to merge different data frames which are characterized by a very large number of variables, which are named with codes such as c302, c303, etc. The original file imported from SPSS keeps very useful labels.

当我尝试合并那些数据框时(使用 cbind merge ),我丢失了所有变量标签。可以保留它们吗?

When I try to merge those data frames (using cbind or merge), I lose all the variable labels. Is it possible to keep them?

推荐答案

而不是使用 merge ,使用dplyr的 left_join 保留属性:

Instead of using merge, use dplyr's left_join as it preserves attributes:

library(dplyr)
library(haven)

df1 <- read_sav("one.sav")
df2 <- read_sav("two.sav")

df <- left_join(x = df1, y = df2, by = "var_name")

这篇关于在R中合并和保留变量标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:46