R:将值标签作为属性添加到非数字列

时间:2015-01-24 21:20:27

标签: r dataframe labels columnattribute

我有大量的SPSS或文本数据文件。使用库read.spss中的foreign导入R中的SPSS文件时,使用use.value.labels = TRUE时会自动添加值标签。它们存储为数据框每列的value.labels属性。我需要保留导入的对象'结构一致无论它们的来源是什么(SPSS或文本)。我需要将value.labels属性及其值分配给从文本文件导入的数据框中的每个非数字列(因子或字符)。以下是从文本文件导入的数据框的摘录:

> mydf <- data.frame(w = factor(c(1, 2, 3)), x = c("fourth", "fifth", "sixth"),
y = c(9.3, 8.8, 2.6), z = factor(c(7, 8, 9)), stringsAsFactors = FALSE)

我可以按列进行以下操作:

> attr(mydf$w, "value.labels") <- c(first = "1", second = "2", third = "3")
> attr(mydf$x, "value.labels") <- c(f4 = "fourth", f5 = "fifth", f6 = "sixth")
> attr(mydf$z, "value.labels") <- c(seventh = "7", eighth = "8", ninth = "9")

然后检查:

> attributes(mydf$w)
$levels
[1] "1" "2" "3"

$class
[1] "factor"

$value.labels
first second  third 
   "1"    "2"    "3" 

但是,由于每个数据帧都包含大量列,因此效率不高。是否可以在给定值标签列表的情况下自动执行此操作,例如:

> lst.attr <- list(w = c(first = "1", second = "2", third = "3"),
x = c(f4 = "fourth", f5 = "fifth", f6 = "sixth"), z = c(seventh = "7",
eighth = "8", ninth = "9"))

0 个答案:

没有答案