[R] as.factor and floating point numbers

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Wed Jan 25 13:24:00 CET 2023


Another option is to convert all times to base units or the sample rate from the analog-to-digital converter. If this is 100 milliseconds then use milliseconds rather than fractions of an hour or day. This approach might not help if the range in values spans more than 16 digits: slightly finer resolution than one year sampled in microseconds.
Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Andrew Simmons
Sent: Wednesday, January 25, 2023 4:13 AM
To: Tobias Fellinger <tobby using htu.at>
Cc: r-help using r-project.org
Subject: Re: [R] as.factor and floating point numbers

[External Email]

R converts floats to strings with ~15 digits of accuracy, specifically to avoid differentiating between 1 and 1 + .Machine$double.eps, it is assumed that small differences such as this are due to rounding errors and are unimportant.

So, if when making your factor, you want all digits, you could write
this: `as.factor(format(x, digits = 17L))`

On Wed, Jan 25, 2023 at 4:03 AM Tobias Fellinger <tobby using htu.at> wrote:
>
> Hello,
>
> I'm encountering the following error:
>
> In a package for survival analysis I use a data.frame is created, one column is created by applying unique on the event times while others are created by running table on the event times and the treatment arm.
>
> When there are event times very close together they are put in the same factor level when coerced to factor while unique outputs both values, leading to different lengths of the columns.
>
> Try this to reproduce:
> x <- c(1, 1+.Machine$double.eps)
> unique(x)
> table(x)
>
> Is there a general best practice to deal with such issues?
>
> Should calling table on floats be avoided in general?
>
> What can one use instead?
>
> One could easily iterate over the unique values and compare all values with the whole vector but this are N*N comparisons, compared to N*log(N) when sorting first and taking into account that the vector is sorted.
>
> I think for my purposes I'll round to a hundredth of a day before calling the function, but any advice on avoiding this issue an writing more fault tolerant code is greatly appreciated.
>
> all the best, Tobias
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu
> %7C0195a6c837ad415067ac08dafeb5360b%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638102351500433338%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sda
> ta=%2FqBEN7jKAYswd1Hz5xAvitS3%2F2TEGm%2FweHoUk80sBgs%3D&reserved=0
> PLEASE do read the posting guide 
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C01
> 95a6c837ad415067ac08dafeb5360b%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638102351500433338%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=kn
> %2BJ8XO8XqOpc5cKXJ6%2FDjIBfN9CH9m6qvimXLTwUBg%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C0195a6c837ad415067ac08dafeb5360b%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638102351500433338%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2FqBEN7jKAYswd1Hz5xAvitS3%2F2TEGm%2FweHoUk80sBgs%3D&reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C0195a6c837ad415067ac08dafeb5360b%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638102351500433338%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=kn%2BJ8XO8XqOpc5cKXJ6%2FDjIBfN9CH9m6qvimXLTwUBg%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list