在另一列的modolus /值之后分配一列的值

时间:2022-05-02 11:12:59

In order to distribute a certain amount of TRUE (=1) values, depending on raster layer's values, I would like to do something like this:

为了分配一定数量的TRUE(= 1)值,取决于栅格图层的值,我想做这样的事情:

Let's say I have a 9x2 matrix looking like this:

假设我有一个9x2矩阵,如下所示:

Example <- matrix(c(0.8,0.6,0.5,0.3,0.2,0.4,0.9,0.7,
                0.1,1,1,1,1,1,0,0,0,0),ncol=2)

     > Example
      [,1] [,2]
 [1,]  0.8    1
 [2,]  0.6    1
 [3,]  0.5    1
 [4,]  0.3    1
 [5,]  0.2    1
 [6,]  0.4    0
 [7,]  0.9    0
 [8,]  0.7    0
 [9,]  0.1    0

Now I want to allocate the 1's from [,2] as a function of the highest values of [,1] without changing the order of [,1]. The result should look like this:

现在我想从[,2]中分配1作为[,1]的最高值的函数,而不改变[,1]的顺序。结果应如下所示:

> Example
      [,1] [,2]
 [1,]  0.8    1
 [2,]  0.6    1
 [3,]  0.5    1
 [4,]  0.3    0
 [5,]  0.2    0
 [6,]  0.4    0
 [7,]  0.9    1
 [8,]  0.7    1
 [9,]  0.1    0

Is there any elegant way to do this. I could't find any function to solve this problem. As I want to convert [,2] to a raster layer using the raster package a raster-based solution would work for me as well.

有没有优雅的方法来做到这一点。我找不到任何解决这个问题的功能。由于我想使用光栅包将[,2]转换为栅格图层,因此基于栅格的解决方案也适用于我。

2 个解决方案

#1


1  

You can find how many 1's you have by summing column 2. You can then find your relative threshold of column 1 by checking if it is higher or lower than that value.

您可以通过对第2列求和来找到您有多少1个。然后,您可以通过检查第1列的相对阈值是高于还是低于该值来找到它的相对阈值。

how.many.ones <- sum(Example[, 2])

max.thresh <- sort(Example[, 1], decreasing=TRUE)[how.many.ones]

change.to.one <- Example[, 1] >= max.thresh

Example[, 2] <- ifelse(change.to.one, 1, 0)

RESULTS:

Example

      [,1] [,2]
 [1,]  0.8    1
 [2,]  0.6    1
 [3,]  0.5    1
 [4,]  0.3    0
 [5,]  0.2    0
 [6,]  0.4    0
 [7,]  0.9    1
 [8,]  0.7    1
 [9,]  0.1    0


Caveat:

There is no tie-handling in this process.

在此过程中没有绑定处理。

#2


0  

I think what you want is something like this:

我想你想要的是这样的:

Example[,2]<-ifelse(Example[,1]>=0.5,1,0)

But, I'm only guessing that you want the column to be 1 if it is greater or equal to 0.5, and 0 if not.

但是,我只是猜测如果列大于或等于0.5,则希望列为1,否则为0。

But maybe you wanted the second column to be the order of the first one. It is a little confusing:

但也许你想要第二列是第一列的顺序。这有点令人困惑:

Example[,2]<-Example[order(Example[,1]),2]

#1


1  

You can find how many 1's you have by summing column 2. You can then find your relative threshold of column 1 by checking if it is higher or lower than that value.

您可以通过对第2列求和来找到您有多少1个。然后,您可以通过检查第1列的相对阈值是高于还是低于该值来找到它的相对阈值。

how.many.ones <- sum(Example[, 2])

max.thresh <- sort(Example[, 1], decreasing=TRUE)[how.many.ones]

change.to.one <- Example[, 1] >= max.thresh

Example[, 2] <- ifelse(change.to.one, 1, 0)

RESULTS:

Example

      [,1] [,2]
 [1,]  0.8    1
 [2,]  0.6    1
 [3,]  0.5    1
 [4,]  0.3    0
 [5,]  0.2    0
 [6,]  0.4    0
 [7,]  0.9    1
 [8,]  0.7    1
 [9,]  0.1    0


Caveat:

There is no tie-handling in this process.

在此过程中没有绑定处理。

#2


0  

I think what you want is something like this:

我想你想要的是这样的:

Example[,2]<-ifelse(Example[,1]>=0.5,1,0)

But, I'm only guessing that you want the column to be 1 if it is greater or equal to 0.5, and 0 if not.

但是,我只是猜测如果列大于或等于0.5,则希望列为1,否则为0。

But maybe you wanted the second column to be the order of the first one. It is a little confusing:

但也许你想要第二列是第一列的顺序。这有点令人困惑:

Example[,2]<-Example[order(Example[,1]),2]