Israel 2008 variable/value labels error

For harmonized variables in the Israel 2008 micro-sample, I use the read_ipums_micro() function in R along with the DDI .xml file (this is my extract #146). In my code, ListFiles[1] is the full path and file name of the .dat file, and ListFiles[3] is the full path and file name for the .xml file.

I get the following error:

rlang::last_error()
<error/vctrs_error_cast_lossy>
Can’t convert from labels to x due to loss of precision.

  • Locations: 24, 39, 45, 53, 57, 59, 63, 64, 67, 69
    Backtrace:
    1. ipumsr::read_ipums_micro(ddi = ListFiles[3], data_file = ListFiles[1])
    2. ipumsr::set_ipums_var_attributes(out, ddi, var_attrs)
    3. purrr::walk(…)
    4. purrr::map(.x, .f, …)
    5. ipumsr:::.f(.x[[i]], …)
    6. ipumsr:::set_single_var_attributes(…)
    7. haven::labelled(x, val_labels)
    8. haven:::vec_cast_named(labels, x, x_arg = “labels”, to_arg = “x”)
  1. vctrs::vec_cast(x, to, …)
  2. vctrs:::vec_cast.integer.double(…)
  3. vctrs::maybe_lossy_cast(out, x, to, lossy, x_arg = x_arg, to_arg = to_arg)
  4. vctrs:::stop_lossy_cast(…)
  5. vctrs:::stop_vctrs(…)

Without seeing your code it’s hard to know what the issue might be, though based on this stack overflow thread it sounds like the error you are getting is related to a for loop. From what I can tell in the error message, your use of ipumsr::read_ipums_micro() should work (see this forum post on reading ipums data in R). A good way to troubleshoot problems with code is to google the error message you are getting; I’ve had good luck finding solutions to troublesome code this way.

Thank you, Grace.Below I’ve copied the R code if there is interest in following up. It is a very simple program that does not use loops.

I’ve done at least 100 extracts over the past 3 weeks using this program, but only for the harmonized variables of Israel 2008 (not the source variables of this microsample!) did I receive any error. This suggests (to me) a possible problem with the generation of the .xml file. The error occurs at the point where the read_ipums_micro() function is invoked, and before the setDT() function that would have converted the data.frame to a data.table.

Otherwise, thanks to the remarkable system that IPUMS-I has devised, every download and conversion has gone flawlessly! The system is really is a thing of beauty.

Mark

***R script follows
rm(list=ls())

library(countrycode)
library(ipumsr)
library(data.table)

Root ← “D:/Research/IPUMS”
PathIn ← file.path(Root, “NewData”)
PathOut ← file.path(Root, “DataExtracts_2021”)

Type ← “Harmonized” # CHOOSE either “Harmonized” or “Source”

#---------------------------------------------------------------------------------------------------

NOTE: UNZIP the .dat file first, before doing anything else:

#---------------------------------------------------------------------------------------------------

ListFiles ← list.files(PathIn, full.names=TRUE)
print(ListFiles)

Set ddi= the .xml file and data_file= the (unzipped) version of the .dat file:

Data ← read_ipums_micro(ddi=ListFiles[3], data_file=ListFiles[1])

setDT(Data)

To name the file, we use the following:

ISOn ← as.integer( Data[1,COUNTRY] ) # COUNTRY is the 3-digit numeric code used by the UN
ISO3 ← countrycode(sourcevar=ISOn, origin=“iso3n”, destination=“iso3c”)
Year ← as.character( Data[1,YEAR] )

save(Data, file=file.path(PathOut, paste0(ISO3,““,Year,””,Type,“.RData”)))

The error you are getting has to do with an issue with the variable INCHH for the Israel 2008 sample. As you suggested, this error occurs because of a problem with the .xml code; INCHH is of type “integer” in DDI codebooks, though it should be of type “double” because it has a few cases with decimals. The IPUMS International team is still working on fixing this issue. Until then, the following workaround should allow you to open an extract with the Israel 2008 sample and the variable INCHH by manually changing the “var_type” of INCHH from “integer” to “double” after reading in the DDI codebook.

library(ipumsr)

xml_file_path ← “/path/to/xml/file.xml”
data_file_path ← “/path/to/data/file.dat.gz”
ddi ← read_ipums_ddi(xml_file_path)
ddi$var_info[ddi$var_info$var_name == “INCHH”, “var_type”] ← “double”
data ← read_ipums_micro(ddi, data_file = data_file_path)

Thanks so much, Grace! That’s an easy work-around.

It certainly speaks well of the IPUMS-I team that glitches like this are incredibly rare!