我使用read_sav函数从haven软件包导入一个SPSS文件。因此,我有列名和关联标签(类labelled)。
我订购数据帧时丢失了标签。我可以避免在顺序之前转换因子的问题,但这是错误还是正常行为?
下面是一个简单的例子。
DataForExample <- structure(list(CollectorNm = structure(c("Email Invitation 8", "Email Invitation 8", "Email Invitation 8", "Email Invitation 8", "Email Invitation 8", "Email Invitation 8"), label = "CollectorNm"), q0001 = structure(c(1, 1, 1, 1, 1, 1), label = "Avez-vous déjà suivi la formation Atlas-Vente des 18 et 19 octobre ?", class = "labelled", labels = structure(c(1, 2), .Names = c("Oui, j'ai bien suivi cette formation.", "Non, je n'y ai pas participé." ))), q0002_0001 = structure(c(3, 3, 3, 2, 3, 3), label = "La formation dans son ensemble", class = "labelled", labels = structure(c(1, 2, 3, 4), .Names = c("pas du tout satisfait", "plutôt pas satisfait", "plutôt satisfait", "très satisfait")))), .Names = c("CollectorNm", "q0001", "q0002_0001"), class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -6L))
View(DataForExample) # OK Toto <- DataForExample[order(DataForExample$q0001_0001),] View(Toto) # NOK : the labels disappeared
谢谢
发布于 2016-11-06 23:23:43
您需要加载包,支持类labelled的子设置操作。最好是在haven之后加载它。至少有两个具有这种支持的包:Hmisc和expss。免责声明:我是expss软件包的作者。
更新。修补程序标记类。
haven似乎没有为所有带有标签的变量设置labelled类。所以我们需要修复它:
library(expss)
for(each in colnames(DataForExample)){
if(!("labelled" %in% class(DataForExample[[each]])) &&
(!is.null(var_lab(DataForExample[[each]])) ||
!is.null(val_lab(DataForExample[[each]]))
)) {
class(DataForExample[[each]]) = union("labelled", class(DataForExample[[each]]))
}
}
View(DataForExample) # OK
Toto <- DataForExample[order(DataForExample$q0001),]
View(Toto) # OKhttps://stackoverflow.com/questions/40452655
复制相似问题