[R] as.factor and floating point numbers

Tobias Fellinger tobby @end|ng |rom htu@@t
Wed Jan 25 10:03:01 CET 2023


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]]



More information about the R-help mailing list