[R] Applying a loop with rnorm() to eliminate duplicate coordinates in a dataframe object

Ben Bolker bolker at ufl.edu
Wed Nov 5 22:46:55 CET 2008


Ricardo Bandin <rbandin <at> gmail.com> writes:

> 
> Dear colleagues
> 
> I found duplicate coordinates trying to convert a dataframe object (see the
> attached file) into a geodata one ('geoR' package is needed). My dataframe
> object consists of 2 columns with UTM geographical coordinates and a 3rd
> column with fish densities .
> 
> To overcome the handicap I must add a tiny value generated by rnorm() to
> each geographical coordinate. And it must be in a loop, otherwise the added
> value will be the same for all the coordinates, persisting the duplicates.
> The example given to me was:
> for (i in 1: length(x$coords[1]) { x$coords[1]+rnorm(1,0,1) }
> 
> But I still can not understand this instruction syntaxis. I'll be very
> grateful if somebody can teach me.
> 

  I don't quite understand the syntax either -- and your attachment
seems to have got(ten) lost in translation.  I'm not quite sure
about the structure of your data frame, but *if* it had three
columns x, y, and z the loop above should be

for (i in 1: length(x$x[i]) { x$x[i]+rnorm(1,0,1) }

(the absence of the loop variable "i" in the statement between
the curly brackets is a clue that something is wrong ...)

but you should be able to do this in a vectorized way as

x$x <- x$x+rnorm(length(x$x),0,1)

I would also suggest that you should adjust the
standard deviation of the noise to be small relative
to the magnitude of your coordinates ...

  Ben Bolker



More information about the R-help mailing list