简体   繁体   中英

Changing duplicated coordinate values by adding a decimal place R

I have UTM coordinate values from GPS collared leopards, and my analysis gets messed up if there are any points that are identical. What I want to do is add a 1 to the end of the decimal string to make each value unique.

What I have:

> View(coords)
> coords
          X       Y
1  623190.9 4980021
2  618876.6 4980729
3  618522.7 4980896
4  618522.7 4980096
5  618522.7 4980096
6  622674.1 4976161

I want something like this, or something that will make each number unique (doesn't have to be a +1)

> coords
          X       Y
1  623190.9 4980021
2  618876.6 4980729
3  618522.7 4980896
4  618522.71 4980096.1
5  618522.72 4977148.2
6  622674.1 4976161

Ive looked at existing questions and got this to work for a simulated data set, but not for values with more than 1 duplicated value.

DF <- data.frame(A=c(5,5,6,6,7,7), B=c(1, 1, 2, 2, 2, 3))

>View(DF)
  A B
1 5 1
2 5 1
3 6 2
4 6 2
5 7 2
6 7 3

DF <- do.call(rbind, lapply(split(DF, list(DF$A, DF$B)), 
  function(x) {
  x$A <- x$A + seq(0, by=0.1, length.out=nrow(x))
  x$B <- x$B + seq(0, by=0.1, length.out=nrow(x))
  x
}))

>View(DF
         A    B
5.1.1  5.0  1.0
5.1.2  5.1  1.1
6.2.3  6.0  2.0
6.2.4  6.1  2.1
7.2    7.0  2.0
7.3    7.0  3.0

The'2s' in column B don't continue to add a decimal place when there are more than 2. I also had a problem accomplishing this when the number was more than 4 digits (ie XXXXX vs XX) There's probably a better way to do this, but I would love help on adding these decimals and possibly altering them in the original data frame which has 12 columns of various data.

It is easier to use make.unique

DF[] <- lapply(DF, function(x) as.numeric(make.unique(as.character(x))))
DF
#    A   B
#1 5.0 1.0
#2 5.1 1.1
#3 6.0 2.0
#4 6.1 2.1
#5 7.0 2.2
#6 7.1 3.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM