Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`write.dbf` fails with an object of class `tbl_df`

Tags:

r

dplyr

I do a lot of my work with .dbf files, and also with dplyr. There's a bug in write.dbf() that prevents writing a tbl_df object to a .dbf file.

Unfortunately, the error message is poorly written and it's therefore difficult to figure out exactly what is happening.

Here's a MWE

library(dplyr)
library(foreign)

d <- data_frame( x = 1:4, y = rnorm(4) )
write.dbf(d, "test.dbf")
  Error in write.dbf(d, "test.dbf") : unknown column type in data frame
like image 609
gregmacfarlane Avatar asked Oct 26 '25 08:10

gregmacfarlane


1 Answers

The solution here is to force the class of d to a bare data.frame

class(d)
 [1] "tbl_df"     "tbl"        "data.frame"
df <- as.data.frame(d)
class(df)
 [1] "data.frame"
write.dbf(as.data.frame(df), "test.dbf")  # works

I've filed a bug report with the foreign people, but hopefully this post can save someone else some pain.

like image 131
gregmacfarlane Avatar answered Oct 28 '25 00:10

gregmacfarlane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!