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

From: Ben Bolker <bolker_at_ufl.edu>
Date: Wed, 05 Nov 2008 21:46:55 +0000 (UTC)

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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 05 Nov 2008 - 21:51:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 05 Nov 2008 - 23:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive