如果数字介于两个数字之间,则分配一个值

时间:2022-11-10 04:31:06

Im trying to assign the value of -1, to every number in my vector that is inbetween 2 and 5. I thought an if - then statement would work. I am having some trouble. I dont think (2

我试图将值-1分配给我的向量中介于2和5之间的每个数字。我认为if - then语句会起作用。我遇到了一些麻烦。我不这么认为(2

x <- c(3.2,6,7.8,1,3,2.5)
if (2<x<5){
    cat(-1)
} else {
    cat (x)
}

5 个解决方案

#1


15  

There are a number of syntax error in your code.

您的代码中存在许多语法错误。

Try using findInterval

尝试使用findInterval

x[findInterval(x, c(2,5)) == 1L] <- -1
x
## [1]  -1.0  6.0  7.8  1.0 -1.0 -1.0

read ?findInterval for more details on the use of findInterval

read?findInterval有关findInterval使用的更多详细信息

You could also use replace

你也可以使用替换

replace(x, x > 2 & x < 5, -1)

Note that

  • for 2<x<5 you need to write x > 2 & x < 5
  • 对于2 2&x <5

  • cat will output to the console or a file / connection. It won't assign anything.
  • cat将输出到控制台或文件/连接。它不会分配任何东西。

#2


9  

You probably just want to replace those elements with -1.

您可能只想用-1替换这些元素。

> x[x > 2 & x < 5] <- -1; x
[1] -1.0  6.0  7.8  1.0 -1.0 -1.0

You could also use ifelse.

你也可以使用ifelse。

> ifelse(x > 2 & x < 5, -1, x)
[1] -1.0  6.0  7.8  1.0 -1.0 -1.0

#3


2  

I compared the solutions with microbenchmark:

我将解决方案与microbenchmark进行了比较:

library(microbenchmark)
library(TeachingDemos)

x = runif(100000) * 1000
microbenchmark(200 %<% x %<% 500
               , x > 200 & x < 500
               , findInterval(x, c(200, 500)) == 1
               , findInterval(x, c(200, 500)) == 1L
               , times = 1000L
               )

Here are the results:

结果如下:

                               expr       min        lq      mean    median        uq       max neval
                  200 %<% x %<% 500 17.089646 17.747136 20.477348 18.910708 21.302945 113.71473  1000
                  x > 200 & x < 500  6.774338  7.092153  8.746814  7.233512  8.284603 103.64097  1000
  findInterval(x, c(200, 500)) == 1  3.578305  3.734023  5.724540  3.933615  6.777687  91.09649  1000
 findInterval(x, c(200, 500)) == 1L  2.042831  2.115266  2.920081  2.227426  2.434677  85.99866  1000

You should take findInterval. Please consider to compare it to 1L instead of 1. It is nearly twice as fast.

你应该使用findInterval。请考虑将其与1L而不是1进行比较。它几乎快了两倍。

#4


1  

Here is another approach that is a little more similar to the original:

这是另一种与原始方法更相似的方法:

library(TeachingDemos)

x <- c(3.2,6,7.8,1,3,2.5)

(x <- ifelse( 2 %<% x %<% 5, -1, x ) )

#5


0  

My preference for assigning a value to a variable based on a clearly defined numeric interval is to use base R syntax:

我希望根据明确定义的数字间隔为变量赋值是使用基本R语法:

 DF$NewVar[DF$LowerLimit <= DF$OriginalVar & DF$OriginalVar < DF$UpperLimit] = "Normal"
 DF$NewVar[DF$LowerLimit < DF$OriginalVar] = "Low"
 DF$NewVar[DF$OriginalVar >= DF$UpperLimit] = "High"

I think this syntax is clearer than any number of R functions, largely because the code can be quickly customized to specify inclusive vs exclusive intervals. In practice, it's quite common to encounter situations where an interval can be defined as either inclusive (i.e., [-x to +x]) or exclusive (i.e., (-x to +x)) or a combination (i.e., [-x to +x)).

我认为这种语法比任何数量的R函数都更清晰,主要是因为代码可以快速定制以指定包容性和独占间隔。实际上,遇到间隔可以定义为包含(即[-x到+ x])或排除(即(-x到+ x))或组合(即[ - ]的情况是很常见的。 x到+ x))。

Additionally, base syntax provides clarity to the code if somebody else is reviewing it later. Each unique library of functions seems to have its own peculiar and slightly different syntax to achieve the same level of specificity as clearly defining the intervals using base R syntax.

此外,如果其他人稍后正在查看代码,则基本语法可以提供代码的清晰度。每个独特的函数库似乎都有自己独特且略有不同的语法,以达到与使用基本R语法明确定义区间相同的特异性水平。

#1


15  

There are a number of syntax error in your code.

您的代码中存在许多语法错误。

Try using findInterval

尝试使用findInterval

x[findInterval(x, c(2,5)) == 1L] <- -1
x
## [1]  -1.0  6.0  7.8  1.0 -1.0 -1.0

read ?findInterval for more details on the use of findInterval

read?findInterval有关findInterval使用的更多详细信息

You could also use replace

你也可以使用替换

replace(x, x > 2 & x < 5, -1)

Note that

  • for 2<x<5 you need to write x > 2 & x < 5
  • 对于2 2&x <5

  • cat will output to the console or a file / connection. It won't assign anything.
  • cat将输出到控制台或文件/连接。它不会分配任何东西。

#2


9  

You probably just want to replace those elements with -1.

您可能只想用-1替换这些元素。

> x[x > 2 & x < 5] <- -1; x
[1] -1.0  6.0  7.8  1.0 -1.0 -1.0

You could also use ifelse.

你也可以使用ifelse。

> ifelse(x > 2 & x < 5, -1, x)
[1] -1.0  6.0  7.8  1.0 -1.0 -1.0

#3


2  

I compared the solutions with microbenchmark:

我将解决方案与microbenchmark进行了比较:

library(microbenchmark)
library(TeachingDemos)

x = runif(100000) * 1000
microbenchmark(200 %<% x %<% 500
               , x > 200 & x < 500
               , findInterval(x, c(200, 500)) == 1
               , findInterval(x, c(200, 500)) == 1L
               , times = 1000L
               )

Here are the results:

结果如下:

                               expr       min        lq      mean    median        uq       max neval
                  200 %<% x %<% 500 17.089646 17.747136 20.477348 18.910708 21.302945 113.71473  1000
                  x > 200 & x < 500  6.774338  7.092153  8.746814  7.233512  8.284603 103.64097  1000
  findInterval(x, c(200, 500)) == 1  3.578305  3.734023  5.724540  3.933615  6.777687  91.09649  1000
 findInterval(x, c(200, 500)) == 1L  2.042831  2.115266  2.920081  2.227426  2.434677  85.99866  1000

You should take findInterval. Please consider to compare it to 1L instead of 1. It is nearly twice as fast.

你应该使用findInterval。请考虑将其与1L而不是1进行比较。它几乎快了两倍。

#4


1  

Here is another approach that is a little more similar to the original:

这是另一种与原始方法更相似的方法:

library(TeachingDemos)

x <- c(3.2,6,7.8,1,3,2.5)

(x <- ifelse( 2 %<% x %<% 5, -1, x ) )

#5


0  

My preference for assigning a value to a variable based on a clearly defined numeric interval is to use base R syntax:

我希望根据明确定义的数字间隔为变量赋值是使用基本R语法:

 DF$NewVar[DF$LowerLimit <= DF$OriginalVar & DF$OriginalVar < DF$UpperLimit] = "Normal"
 DF$NewVar[DF$LowerLimit < DF$OriginalVar] = "Low"
 DF$NewVar[DF$OriginalVar >= DF$UpperLimit] = "High"

I think this syntax is clearer than any number of R functions, largely because the code can be quickly customized to specify inclusive vs exclusive intervals. In practice, it's quite common to encounter situations where an interval can be defined as either inclusive (i.e., [-x to +x]) or exclusive (i.e., (-x to +x)) or a combination (i.e., [-x to +x)).

我认为这种语法比任何数量的R函数都更清晰,主要是因为代码可以快速定制以指定包容性和独占间隔。实际上,遇到间隔可以定义为包含(即[-x到+ x])或排除(即(-x到+ x))或组合(即[ - ]的情况是很常见的。 x到+ x))。

Additionally, base syntax provides clarity to the code if somebody else is reviewing it later. Each unique library of functions seems to have its own peculiar and slightly different syntax to achieve the same level of specificity as clearly defining the intervals using base R syntax.

此外,如果其他人稍后正在查看代码,则基本语法可以提供代码的清晰度。每个独特的函数库似乎都有自己独特且略有不同的语法,以达到与使用基本R语法明确定义区间相同的特异性水平。