1

I have created a data.table that looks, for example as the following:

a b c exposure
1 2 3 0.5
1 2 3 0.5
4 5 6 0.75
4 5 6 0.25

I am summing the values in the exposure column by unique combinations of a, b, and c variables. Using data.table, my command is

data1 <- data[, .(final = sum(exposure)), by = list(a, b, c)]

As expected, this results in:

a b c final
1 2 3 1
4 5 6 1

However, when I try to retrieve the unique values using

unique(data1$final)

I get: 1 1

I have tried using str and class to see if the 1s are different data types, but they are both numeric. When I try

unique(data1$final) == c(1,1) ## the result is TRUE FALSE

Do you have any suggestions on how to debug this, or why unique is returning of the same value? Ideally I am trying to create a check that says there is only one unique value (equal to 1) of the summed rows.

pau
  • 51
  • 1
  • 1
  • 5
  • 1
    Not able to reproduce the problem. `data1 <- data[, .(final = sum(exposure)), by = list(a, b, c)]; unique(data1$final)# [1] 1` You may check whether the 'final' is float and if the values are not exactly equal to 1? – akrun Mar 01 '18 at 09:41
  • Thank you! You were correct this seems to be the same issue as: https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal I tried c(1,1) == data1$final, and the result is TRUE FALSE But when I tried all.equal(c(1,1), data1$final), the result is TRUE – pau Mar 01 '18 at 09:46

0 Answers0