I know how to import a simple csv file using R. But, is it possible to import a file to R including variable and value labels (similar to SPSS sav files).
Or instead, shall I have two csv files? One for data and the other for metadata (variable and value labels)?
Something similar to this (resulting from two csv files). But I think I have a problem with the syntax of the tuples for val_lab:
> data
# A tibble: 6 × 2
se ctr
<chr> <chr>
1 1 1
2 1 2
3 2 3
4 2 2
5 1 1
6 2 3
> metadata
# A tibble: 2 × 3
var var_label val_lab
<chr> <chr> <chr>
1 se sex (1,'Female'),(2,'Male')
2 ctr country (1,'UK'),(2,'USA'),(3,'France')
Using dput:
> dput(head(data))
structure(list(se = c("1", "1", "2", "2", "1", "2"), ctr = c("1",
"2", "3", "2", "1", "3")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
> dput(metadata)
structure(list(var = c("se", "ctr"), var_label = c("sex", "country"
), val_lab = c("(1,'Female'),(2,'Male')", "(1,'UK'),(2,'USA'),(3,'France')"
)), row.names = c(NA, -2L), spec = structure(list(cols = list(
var = structure(list(), class = c("collector_character",
"collector")), var_label = structure(list(), class = c("collector_character",
"collector")), val_lab = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ";"), class = "col_spec"), problems = <pointer: 0x00000149af86d620>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
havenpackage does it with data imported from SPSS or Stata by setting attributes. But csv files don't usually have labels in them - if you give a (minimal) example of a csv file and where the labels are, it will be clearer how specifically to advise you.dput(head(data))anddput(metadata)?RDS(single object) orRdata(multiple object) files. This can include columns offactorclass, which is R's standard class for categorical data.factors have integer values and a "levels" attribute that provides name labels.