-1

I'm looping through a sequence of 1 to 1.9 by increments of 0.1 to find these valued positions in my data (datapositions1). However, when looping through, the value "1.7" is not being picked up although its definitely in the data. However, if I use a sequence of (1, 1.7, by=0.1) then it picks up the "1.7" & stranger again if I use a sequence of (1.1, 1.9, by=0.1) then it neither picks up "1.7" nor "1.8".

An snippit of my data:

name    position  value
Name A      1.8  458.77011494
Name A      3.3  352.10734463
Name A      1.7  167.26923077
Name A      1.5   14.19756839
Name A      3.0   96.47292419
Name A      1.9  636.25490196
Name A      1.7    1.81479312
Name A      1.2  130.94444444
Name A      1.6   43.66501241
Name B      1.2   86.40421456
Name B      1.1   48.13294798
Name B      1.0    3.28143556
Name C      1.7  460.53846154
Name C      1.8  501.41545894
Name C      1.0  112.03095752
Name C      1.7  216.39130435

The function I'm using:

Average= function(){ 
  df = data.frame()
  for (i in seq(1, 1.9, 0.1)){
    #print(i)
    #b = datapositions1[c(datapositions1$position == 1.7),]
    #print(b)
    p = datapositions1[c(datapositions1$position == i),]
    print(p)
    df = rbind(df, data.frame(p))
  }
 df
}
Average()

Also in the code above where i used "b" it always returns correct data when using "1.7" whereas the "p" using "i" loses the "1.7" data.

This is the output of "p" losing "1.7" data.

497   Name A      1.6  60.997704
543   Name C      1.6 146.058824
544   Name C      1.6 163.133739
[1] 1.7
[1] name position      value
<0 rows> (or 0-length row.names)
[1] 1.8
    name position        value
42    Name A      1.8  20.614468
43    Name B      1.8  49.724638
89    Name A      1.8 101.725367

Any advise please :)

lmo
  • 37,904
  • 9
  • 56
  • 69

1 Answers1

2

I think I have a current solution.

I read this in the documentation.. "The second form generates from, from+by, ..., up to the sequence value less than or equal to to. Specifying to - from and by of opposite signs is an error. Note that the computed final value can go just beyond to to allow for rounding error, but is truncated to to. (‘Just beyond’ is by up to 1e-10 times abs(from - to).)"

I noticed it could be a rounding issue and that the seq code might be doing something like:

a+=0.1 
> 0.1
a+=0.1 
> 0.2000000002

Meaning "i" would never equal what is represented in the table. Therefore using:

i = as.character(i)

Seemed to solve the problem!