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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With