将标记为 SPSS 数据的标签和值导出到 Json
Export labels and values labeled SPSS data into Json
我有一个带标签的大型 SPSS 文件,它有大约 800 个变量并包含所有元数据、标签、值和变量。我正在寻找一种方法将它写成 json 格式而不丢失变量 txt、标签和值。
这里是一个示例虚拟数据:
library(haven)
library(labelled)
df <- data.frame(
a = labelled(c(1, 1, 2, 3), labels = c(No = 1, Yes = 2)),
b = labelled(c(1, 1, 2, 3), labels = c(No = 1, Yes = 2, DK = 3)),
c = labelled(c(1, 1, 2, 2), labels = c(No = 1, Yes = 2, DK = 3)),
d = labelled(c("a", "a", "b", "c"), labels = c(No = "a", Yes = "b")),
e = labelled_spss(
c(1, 9, 1, 2),
labels = c(No = 1, Yes = 2),
na_values = 9 ))
df1 <- df %>%
set_variable_labels( a = "txt1- Do you use xxx?") %>%
set_variable_labels( b = "txt2-Do you use xxx?") %>%
set_variable_labels( c = "txt3-Do you use xxx?") %>%
set_variable_labels( d = "txt4-Do you use xxx?") %>%
set_variable_labels( e = "txt5-Do you use xxx?")
如果我用
jsonlite::write_json()
我可以将文件导出为 json,但我会丢失标签和其他元数据。有什么方法可以将其与元数据变量标签、值标签等一起导出到 json 文件
library(jsonlite)
exportJSON <- toJSON(df1)
write_json(exportJSON, "output.json")
serializeJSON()
允许将 R 对象转换为 JSON,保持所有数据和属性(但不包括环境)。可以用unserializeJSON()
.
恢复数据
library(labelled)
library(jsonlite)
library(tibble)
df1 <- df1 %>%
as_tibble() # For prettier printing of labels
# Write json file
write(serializeJSON(df1), file = "dat.json")
# Restore
unserializeJSON(readLines("dat.json"))
# A tibble: 4 x 5
a b c d e
* <dbl+lbl> <dbl+lbl> <dbl+lbl> <chr+lbl> <dbl+lbl>
1 1 [No] 1 [No] 1 [No] a [No] 1 [No]
2 1 [No] 1 [No] 1 [No] a [No] 9 (NA)
3 2 [Yes] 2 [Yes] 2 [Yes] b [Yes] 1 [No]
4 3 3 [DK] 2 [Yes] c 2 [Yes]
我有一个带标签的大型 SPSS 文件,它有大约 800 个变量并包含所有元数据、标签、值和变量。我正在寻找一种方法将它写成 json 格式而不丢失变量 txt、标签和值。
这里是一个示例虚拟数据:
library(haven)
library(labelled)
df <- data.frame(
a = labelled(c(1, 1, 2, 3), labels = c(No = 1, Yes = 2)),
b = labelled(c(1, 1, 2, 3), labels = c(No = 1, Yes = 2, DK = 3)),
c = labelled(c(1, 1, 2, 2), labels = c(No = 1, Yes = 2, DK = 3)),
d = labelled(c("a", "a", "b", "c"), labels = c(No = "a", Yes = "b")),
e = labelled_spss(
c(1, 9, 1, 2),
labels = c(No = 1, Yes = 2),
na_values = 9 ))
df1 <- df %>%
set_variable_labels( a = "txt1- Do you use xxx?") %>%
set_variable_labels( b = "txt2-Do you use xxx?") %>%
set_variable_labels( c = "txt3-Do you use xxx?") %>%
set_variable_labels( d = "txt4-Do you use xxx?") %>%
set_variable_labels( e = "txt5-Do you use xxx?")
如果我用
jsonlite::write_json()
我可以将文件导出为 json,但我会丢失标签和其他元数据。有什么方法可以将其与元数据变量标签、值标签等一起导出到 json 文件
library(jsonlite)
exportJSON <- toJSON(df1)
write_json(exportJSON, "output.json")
serializeJSON()
允许将 R 对象转换为 JSON,保持所有数据和属性(但不包括环境)。可以用unserializeJSON()
.
library(labelled)
library(jsonlite)
library(tibble)
df1 <- df1 %>%
as_tibble() # For prettier printing of labels
# Write json file
write(serializeJSON(df1), file = "dat.json")
# Restore
unserializeJSON(readLines("dat.json"))
# A tibble: 4 x 5
a b c d e
* <dbl+lbl> <dbl+lbl> <dbl+lbl> <chr+lbl> <dbl+lbl>
1 1 [No] 1 [No] 1 [No] a [No] 1 [No]
2 1 [No] 1 [No] 1 [No] a [No] 9 (NA)
3 2 [Yes] 2 [Yes] 2 [Yes] b [Yes] 1 [No]
4 3 3 [DK] 2 [Yes] c 2 [Yes]