Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing in r, problems with some points

I created functions dyst and dystryb:

dyst<- function(t,x)
{
  f<-1
  return(f)
}
dystryb<- function(x)
{
  x<-sort(x)
  s<- numeric(101)
  u<-seq(0,1, by = 0.01)
  for (t in u)
  {
    s[t*100+1]<-dyst(t,x)
  }
  return(s)
}

After calling function dystryb I get this:

> x<-c(1,2,3,4,5,6,7)
> dystryb(x)
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [51] 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[101] 1

Why isn't this function working for argument 30 and 59 ? Of course it is not about making a functions, which make vector of "1", but I wanted to make it clear, where the problem is.

like image 617
Aga Avatar asked Oct 29 '22 14:10

Aga


1 Answers

The following shows exactly what happened, you will have zeros at the locations 15, 29,... because of floating point precision error.

which(seq(0,1, by = 0.01)*100+1 != 1:101)
# [1] 15 29 30 56 57 58 59
like image 190
Sandipan Dey Avatar answered Nov 15 '22 06:11

Sandipan Dey