How to import .dbf files perfectly

Hi, and welcome!

Normally, a reproducible example, called a reprex would be helpful, and it's always a good idea to include one, if possible. However, it's not always possible to run one, so I have to cut-and-paste.

Here's how it should work

> 
> library(foreign)
> # from example
> x <- read.dbf(system.file("files/sids.dbf", package="foreign")[1])
> str(x)
'data.frame':	100 obs. of  14 variables:
 $ AREA     : num  0.114 0.061 0.143 0.07 0.153 0.097 0.062 0.091 0.118 0.124 ...
 $ PERIMETER: num  1.44 1.23 1.63 2.97 2.21 ...
 $ CNTY_    : int  1825 1827 1828 1831 1832 1833 1834 1835 1836 1837 ...
 $ CNTY_ID  : int  1825 1827 1828 1831 1832 1833 1834 1835 1836 1837 ...
 $ NAME     : Factor w/ 100 levels "Alamance","Alexander",..: 5 3 86 27 66 46 15 37 93 85 ...
 $ FIPS     : Factor w/ 100 levels "37001","37003",..: 5 3 86 27 66 46 15 37 93 85 ...
 $ FIPSNO   : int  37009 37005 37171 37053 37131 37091 37029 37073 37185 37169 ...
 $ CRESS_ID : int  5 3 86 27 66 46 15 37 93 85 ...
 $ BIR74    : num  1091 487 3188 508 1421 ...
 $ SID74    : num  1 0 5 1 9 7 0 0 4 1 ...
 $ NWBIR74  : num  10 10 208 123 1066 ...
 $ BIR79    : num  1364 542 3616 830 1606 ...
 $ SID79    : num  0 3 6 2 3 5 2 2 2 5 ...
 $ NWBIR79  : num  19 12 260 145 1197 ...
 - attr(*, "data_types")= chr  "N" "N" "N" "N" ...
> summary(x)
      AREA         PERIMETER         CNTY_         CNTY_ID            NAME         FIPS        FIPSNO         CRESS_ID    
 Min.   :0.042   Min.   :0.999   Min.   :1825   Min.   :1825   Alamance : 1   37001  : 1   Min.   :37001   Min.   :  1.0  
 1st Qu.:0.091   1st Qu.:1.324   1st Qu.:1902   1st Qu.:1902   Alexander: 1   37003  : 1   1st Qu.:37050   1st Qu.: 25.8  
 Median :0.120   Median :1.608   Median :1982   Median :1982   Alleghany: 1   37005  : 1   Median :37100   Median : 50.5  
 Mean   :0.126   Mean   :1.673   Mean   :1986   Mean   :1986   Anson    : 1   37007  : 1   Mean   :37100   Mean   : 50.5  
 3rd Qu.:0.154   3rd Qu.:1.859   3rd Qu.:2067   3rd Qu.:2067   Ashe     : 1   37009  : 1   3rd Qu.:37150   3rd Qu.: 75.2  
 Max.   :0.241   Max.   :3.640   Max.   :2241   Max.   :2241   Avery    : 1   37011  : 1   Max.   :37199   Max.   :100.0  
                                                               (Other)  :94   (Other):94                                  
     BIR74           SID74          NWBIR74         BIR79           SID79          NWBIR79     
 Min.   :  248   Min.   : 0.00   Min.   :   1   Min.   :  319   Min.   : 0.00   Min.   :    3  
 1st Qu.: 1077   1st Qu.: 2.00   1st Qu.: 190   1st Qu.: 1336   1st Qu.: 2.00   1st Qu.:  250  
 Median : 2180   Median : 4.00   Median : 698   Median : 2636   Median : 5.00   Median :  874  
 Mean   : 3300   Mean   : 6.67   Mean   :1051   Mean   : 4224   Mean   : 8.36   Mean   : 1353  
 3rd Qu.: 3936   3rd Qu.: 8.25   3rd Qu.:1168   3rd Qu.: 4889   3rd Qu.:10.25   3rd Qu.: 1407  
 Max.   :21588   Max.   :44.00   Max.   :8027   Max.   :30757   Max.   :57.00   Max.   :11631  

However,

The DBF format is documented but not much adhered to. There is is no guarantee this will read all DBF files.

You might have better luck with the RODBC package

1 Like