1

Why would the last two rows in df1 and df2 fail to merge in the following? (See printed output below) I can reproduce this with differing inputs and it's always the last row that doesn't match (although in some cases it works fine).

df1 <- cbind.data.frame(seq(0.2, 0.3, 0.02), 1:6)
colnames(df1) <- c("trt", "id1")
print(df1)

df2 <- data.frame(7:12)
df2$trt <- 0.2 + 0.02*(as.numeric(rownames(df2)) - 1)
colnames(df2) <- c("id2", "trt")
df2 <- df2[c(2, 1)]
print(df2)

mergedf <- merge(df1, df2, all = T)
print(mergedf)

Printed Output

   trt id1
1 0.20   1
2 0.22   2
3 0.24   3
4 0.26   4
5 0.28   5
6 0.30   6

   trt id2
1 0.20   7
2 0.22   8
3 0.24   9
4 0.26  10
5 0.28  11
6 0.30  12

1 0.20   1   7
2 0.22   2   8
3 0.24   3   9
4 0.26   4  10
5 0.28   5  11
6 0.30   6  NA
7 0.30  NA  12
  • Trying to match on floating point numbers is not a good idea. They are actually not the same number. Compare `sprintf("%.20f",df1[6,"trt"])` and `sprintf("%.20f",df2[6,"trt"])`. you could explicitly do a `round(,2)` on your decimals if you only care about the first two digits of the decimal. – MrFlick Dec 10 '18 at 17:31

0 Answers0