[R] Re: read dbf files into R

Kevin Bartz kbartz at loyaltymatrix.com
Wed Sep 29 07:27:38 CEST 2004


Vikas Rawal wrote:
> 
> Is there a linux-based/free command line tool for converting dbf files 
> into txt? Conceptually, it is not a great way of doing things. We have a 
> dbf file with a well defined structure. We convert it into a text file, 
> which has a loose structure, undefined variables types etc. And then we 
> read the text file. I should be much better to directly read a dbf file 
> and use its database structure definition to ensure that data come into 
> R correctly.
> 
> RODBC route does not seem suitable for my needs. I need to read some 300 
> files, and combine all the data. Using ODBC would mean that I would have 
> to set up 300 DSNs in the odbc.ini.
> 
> Or is there a way to set it up from the command line as well? I suppose 
> it must be possible to write a script that will suitably modify odbc.ini 
> file. But that sounds far too complicated.
> 
> I have been a user of SAS for a long time. This exercise would be done 
> in a flash there. I wish there was a simple way of doing it in R.
> 
> Don't we have a simple command that will read a dbf file, or in fact, a 
> set of commands that will read common file formats. I see that we can 
> read SAS, STATA and SPSS files. Somebody would have thought of doing the 
> same for dbf. Isn't it?
> 
> Vikas
> 
> Vikas
> 
> 
> 
> Vito Ricci wrote:
> 
>> Hi,
>>
>> read the manual: R Data Import/Export
>> http://cran.r-project.org/doc/manuals/R-data.pdf
>>
>> Another way is to convert .dbf file in .txt and use
>> read.table(), scan() an similar.
>>
>> Best
>> Vito
>>
>>
>> You wrote:
>>
>> I run R on redhat linux.
>> What would be the easiest way to read dbf files into
>> R?
>>
>> Vikas
>>
>> =====
>> Diventare costruttori di soluzioni
>>
>> "The business of the statistician is to catalyze the scientific 
>> learning process."  George E. P. Box
>>
>>
>> Visitate il portale http://www.modugno.it/
>> e in particolare la sezione su Palese 
>> http://www.modugno.it/archivio/cat_palese.shtml
>>
>>
>>        
>> ___________________________________
>>
>>
>>  
>>
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 

Hi Vikas,

I use dbf.read from the maptools package available on CRAN. The package 
itself is intended to read Arcview shapefiles, but dbf.read can read a 
general dbf and throw it into an R data frame. Because dbf.read's not in 
maptools' namespace, however, you'll have to access it directly; e.g.,

## Non-trivial example: Read ZIP-county mapping file from www.census.gov
 > zipnov <- maptools:::dbf.read("/home/kevin/census/zipnov99.DBF")
 > head(zipnov)
   ZIP_CODE   LATITUDE   LONGITUDE ZIP_CLASS     PONAME STATE COUNTY
1    00210 +43.005895 -071.013202         U PORTSMOUTH    33    015
2    00211 +43.005895 -071.013202         U PORTSMOUTH    33    015
3    00212 +43.005895 -071.013202         U PORTSMOUTH    33    015
4    00213 +43.005895 -071.013202         U PORTSMOUTH    33    015
5    00214 +43.005895 -071.013202         U PORTSMOUTH    33    015
6    00215 +43.005895 -071.013202         U PORTSMOUTH    33    015

 From there, you could write it out into a text file:

## Write to csv with usual options set
write.table(zipnov, row.names = F, sep = ",", file = "zipnov.csv")

Let me know if you have any questions.

Kevin




More information about the R-help mailing list