如何修复R中出现的‘Error in colSums(is.na(codes))’报错?
colSums(is.na(codes)) Error with LET171 SDMX Data in R This error pops up because one (or more) of the codelists in the LET171 dataset has an unexpected structure—either it's empty, or the rsdmx package can't parse it into a dataframe cleanly. Unlike the RV021 dataset, LET171 has some codelists that return NULL when you try to convert them, which breaks the internal colSums check used by as.data.frame.
Here's how to fix it step by step:
1. First, Identify the Problematic Codelist
Let's add some debugging to find exactly which codelist is causing the issue. Run this modified loop to print status updates and catch errors:
library(stringr) library('rsdmx') # Load the data structure dsd = readSDMX("http://andmebaas.stat.ee/restsdmx/sdmx.ashx/GetDataStructure/LET171") cls = slot(dsd, "codelists") codelists <- sapply(slot(cls, "codelists"), function(x) slot(x, "id")) # Loop through codelists with error checking for(i in codelists){ kood <- str_sub(i, start= 10) if(kood != "OBS_STATUS"){ cat("Processing codelist:", i, "(short ID:", kood, ")\n") tryCatch({ cl_data <- as.data.frame(slot(dsd, "codelists"), codelistId = i) assign(kood, cl_data) cat("✅ Successfully processed:", i, "\n") }, error = function(e){ cat("❌ Error with", i, ":", e$message, "\n") }) } }
This will print exactly which codelist is failing, so you can decide whether to skip it or handle it separately.
2. Use a More Robust Codelist Extraction Method
Instead of directly manipulating slots (which is fragile), use the rsdmx package's built-in getCodelist function—it's designed to handle edge cases better. Here's the full revised code:
library(stringr) library('rsdmx') # Load and convert the main dataset (your original code works here) data = readSDMX('http://andmebaas.stat.ee/restsdmx/sdmx.ashx/GetData/LET171/1+2+3+4+5+6+7+8+9+10+11+12+13+14.1+2+3/all?startTime=2010&endTime=2016') DF = as.data.frame(data) # Load the data structure dsd = readSDMX("http://andmebaas.stat.ee/restsdmx/sdmx.ashx/GetDataStructure/LET171") cls = slot(dsd, "codelists") codelists <- sapply(slot(cls, "codelists"), function(x) slot(x, "id")) # Loop through codelists with safe handling for(i in codelists){ kood <- str_sub(i, start= 10) if(kood != "OBS_STATUS"){ # Try to get the codelist, return NULL if it fails cl <- tryCatch(getCodelist(dsd, i), error = function(e) NULL) # Only assign if we got valid, non-empty data if(!is.null(cl) && nrow(cl) > 0){ assign(kood, as.data.frame(cl)) } else { cat("Skipping empty/invalid codelist:", i, "\n") } } }
This code skips any codelists that can't be parsed, preventing the error while preserving all valid data.
3. Verify and Adjust
After running the revised code:
- Check the main dataframe
DFto confirm it loaded correctly - Inspect the generated codelist dataframes (e.g., using
head(your_codelist_name)) - If you need the problematic codelist, use
str(getCodelist(dsd, "problematic_codelist_id"))to examine its structure and write custom parsing logic for it.
内容的提问来源于stack exchange,提问作者Raul

