R subsetting functions, including '[' not accessing certain values in dataframe or matrix

StackOverflow https://stackoverflow.com/questions/22358939

  •  13-06-2023
  •  | 
  •  

Question

Same problem as here: R subset functions, including '[' not working on middle range of large dataframe/matrix, but someone marked that question as duplicate before I had a chance to try out the answer this morning. Specifically I cannot access .937-.975 for my variables, either in a one-liner or in a for loop. To illustrate the problem using '[':

Getting the same problem with '[', where a large part of the matrix, between .937 and .975 is not getting processed:

> for3d.mat <- matrix(ncol=3,nrow=0)
> for(i in seq(.8,1,by=.001)){for(j in seq(.8,1,by=.001)){iter.mat <- matrix(ncol=3,c(i,j,length(MergedBH[MergedBH$V4.x ==i & MergedBH$V4.y ==j,]$V4.x)));for3d.mat <- rbind(for3d.mat,iter.mat)}}
> for3d.mat[for3d.mat[,1] == .975 & for3d.mat[,2] == .966,]
 [,1] [,2] [,3]

Am able to use '[' or subset on most of the matrix, but there is just a specific range whether for the original data frame or the for3d.mat that is not accessible by either subsetting method, example below:

> for3d.mat[for3d.mat[,1] == .976 & for3d.mat[,2] == .937,]
[1] 0.976 0.937    NA
> for3d.mat[for3d.mat[,1] == .975 & for3d.mat[,2] == .937,]
 [,1] [,2] [,3]

Editing to show the first answer is right and it is a FAQ 7.31 issue:

> unique(for3d.mat[,1] - 0.976, digits=16)
[1] -5.600000e-02 -5.500000e-02 -5.400000e-02 -5.300000e-02 -5.200000e-02
[6] -5.100000e-02 -5.000000e-02 -4.900000e-02 -4.800000e-02 -4.700000e-02
[11] -4.600000e-02 -4.500000e-02 -4.400000e-02 -4.300000e-02 -4.200000e-02
[16] -4.100000e-02 -4.000000e-02 -3.900000e-02 -3.800000e-02 -3.700000e-02
[21] -3.600000e-02 -3.500000e-02 -3.400000e-02 -3.300000e-02 -3.200000e-02
[26] -3.100000e-02 -3.000000e-02 -2.900000e-02 -2.800000e-02 -2.700000e-02
[31] -2.600000e-02 -2.500000e-02 -2.400000e-02 -2.300000e-02 -2.200000e-02
[36] -2.100000e-02 -2.000000e-02 -1.900000e-02 -1.800000e-02 -1.700000e-02
[41] -1.600000e-02 -1.500000e-02 -1.400000e-02 -1.300000e-02 -1.200000e-02
[46] -1.100000e-02 -1.000000e-02 -9.000000e-03 -8.000000e-03 -7.000000e-03
[51] -6.000000e-03 -5.000000e-03 -4.000000e-03 -3.000000e-03 -2.000000e-03
[56] -1.000000e-03  1.110223e-16  1.000000e-03  2.000000e-03  3.000000e-03
[61]  4.000000e-03  5.000000e-03  6.000000e-03  7.000000e-03  8.000000e-03
[66]  9.000000e-03  1.000000e-02  1.100000e-02  1.200000e-02  1.300000e-02
[71]  1.400000e-02

1.11e-16 corresponds to the values that should be .976

Was it helpful?

Solution

I expect that this is FAQ 7.31.

Look at the result of:

for3d.mat[,1] == 0.976
for3d.mat[,2] == 0.937
print(for3d.mat[,1] - 0.976, digits=16)
print(for3d.mat[,2] - 0.937, digits=16)

And then read the FAQ.

I would have run the above myself to confirm, but we don't have MergedBH and cannot therefore reproduce your example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top