0

In python there's the decimal module whereby you can store decimal values without fear of falling into the floating point trap. Does R have a similar package?

In particular I'm looking for a way to save an R object to an arrow table/parquet file where one or more columns are Decimal type.

If I do

abc=data.table(a=c(1.23,2.34,3.45))
as_arrow_table(abc, schema=schema(field('a', decimal(3,2))))

I get Error: NotImplemented: Extend

Dean MacGregor
  • 11,847
  • 9
  • 34
  • 72
  • 1
    R doesn't have a decimal type, so if floating point is unworkable you might consider expressing the number as integer cents. (or hundredths if not dollars) – Jon Spring Aug 11 '22 at 18:40
  • Does this answer your question? https://stackoverflow.com/questions/16960167/is-there-a-datatype-decimal-in-r – Jon Spring Aug 11 '22 at 18:41

1 Answers1

4

Because https://issues.apache.org/jira/browse/ARROW-11631 has not yet been done, you can't pass the schema in like that to the conversion function. But you can create the table and then cast it to decimal like this:

arrow_table(abc)$cast(schema(a = decimal(3, 2)))

# Table
# 3 rows x 1 columns
# $a <decimal128(3, 2)>
Neal Richardson
  • 792
  • 3
  • 3