The function reads a DBF file into a data frame, converting character fields to factors, and trying to respect NULL fields.

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

read.dbf(file, as.is = FALSE)

Arguments

file

name of input file

as.is

should character vectors not be converted to factors?

Details

DBF is the extension used for files written for the ‘XBASE’ family of database languages, ‘covering the dBase, Clipper, FoxPro, and their Windows equivalents Visual dBase, Visual Objects, and Visual FoxPro, plus some older products’ (https://www.clicketyclick.dk/databases/xbase/format/). Most of these follow the file structure used by Ashton-Tate's dBase II, III or 4 (later owned by Borland).

read.dbf is based on C code from http://shapelib.maptools.org/ which implements the ‘XBASE’ specification. It can convert fields of type "L" (logical), "N" and "F" (numeric and float) and "D" (dates): all other field types are read as-is as character vectors. A numeric field is read as an R integer vector if it is encoded to have no decimals, otherwise as a numeric vector. However, if the numbers are too large to fit into an integer vector, it is changed to numeric. Note that is possible to read integers that cannot be represented exactly even as doubles: this sometimes occurs if IDs are incorrectly coded as numeric.

Value

A data frame of data from the DBF file; note that the field names are adjusted to use in R using make.names(unique=TRUE).

There is an attribute "data_type" giving the single-character dBase types for each field.

Author

Nicholas Lewin-Koh and Roger Bivand; shapelib by Frank Warmerdam

Note

Not to be able to read a particular ‘DBF’ file is not a bug: this is a convenience function especially for shapefiles.

See also

Examples

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 [1:14] "N" "N" "N" "N" ...
summary(x)
#>       AREA          PERIMETER         CNTY_         CNTY_ID            NAME   
#>  Min.   :0.0420   Min.   :0.999   Min.   :1825   Min.   :1825   Alamance : 1  
#>  1st Qu.:0.0910   1st Qu.:1.324   1st Qu.:1902   1st Qu.:1902   Alexander: 1  
#>  Median :0.1205   Median :1.609   Median :1982   Median :1982   Alleghany: 1  
#>  Mean   :0.1263   Mean   :1.673   Mean   :1986   Mean   :1986   Anson    : 1  
#>  3rd Qu.:0.1542   3rd Qu.:1.859   3rd Qu.:2067   3rd Qu.:2067   Ashe     : 1  
#>  Max.   :0.2410   Max.   :3.640   Max.   :2241   Max.   :2241   Avery    : 1  
#>                                                                 (Other)  :94  
#>       FIPS        FIPSNO         CRESS_ID          BIR74           SID74      
#>  37001  : 1   Min.   :37001   Min.   :  1.00   Min.   :  248   Min.   : 0.00  
#>  37003  : 1   1st Qu.:37050   1st Qu.: 25.75   1st Qu.: 1077   1st Qu.: 2.00  
#>  37005  : 1   Median :37100   Median : 50.50   Median : 2180   Median : 4.00  
#>  37007  : 1   Mean   :37100   Mean   : 50.50   Mean   : 3300   Mean   : 6.67  
#>  37009  : 1   3rd Qu.:37150   3rd Qu.: 75.25   3rd Qu.: 3936   3rd Qu.: 8.25  
#>  37011  : 1   Max.   :37199   Max.   :100.00   Max.   :21588   Max.   :44.00  
#>  (Other):94                                                                   
#>     NWBIR74           BIR79           SID79          NWBIR79       
#>  Min.   :   1.0   Min.   :  319   Min.   : 0.00   Min.   :    3.0  
#>  1st Qu.: 190.0   1st Qu.: 1336   1st Qu.: 2.00   1st Qu.:  250.5  
#>  Median : 697.5   Median : 2636   Median : 5.00   Median :  874.5  
#>  Mean   :1050.8   Mean   : 4224   Mean   : 8.36   Mean   : 1352.8  
#>  3rd Qu.:1168.5   3rd Qu.: 4889   3rd Qu.:10.25   3rd Qu.: 1406.8  
#>  Max.   :8027.0   Max.   :30757   Max.   :57.00   Max.   :11631.0  
#>