FOR循环中的IF语句

时间:2022-11-11 19:37:34

I'm trying to right a function that would take all the elements which are more than 0.5 or less than -0.5 from rnorm(), double it and return it into a vector.

我试图找一个能从rnorm()获取大于0.5或小于-0.5的所有元素的函数,将它加倍并将其返回到向量中。

zzz<-function(xxx=2){
    zz<-rnorm(1000)

    maxi<-zz>0.5
    mini<-zz<-0.5

    for (i in 1:zz){
           if (maxi==TRUE | mini==TRUE){       
          yyy<-xxx*zz[i]
          count<-append(count,yyy)
                   }
    }
count
}

I'm very new to this. Any help will be much appreciated. Thank you

我对此很新。任何帮助都感激不尽。谢谢

2 个解决方案

#1


3  

Using a loop in this way is not idiomatic R. R is a vectorized language which means loops are not needed to do arithmetic on vectors. I recommend reading An Introduction to R to get familiar with the standard approaches.

以这种方式使用循环不是惯用的R.R是一种矢量化语言,这意味着不需要循环来对矢量进行算术运算。我建议阅读R简介以熟悉标准方法。

Also I don't think it's optimal to compute your random vector inside the function (it will make it hard to debug); maybe you want to pass it as an argument.

另外我认为在函数内部计算随机向量并不是最优的(它会使调试变得困难);也许你想把它作为一个参数传递。

The following code will do what you want:

以下代码将执行您想要的操作:

v <- rnorm(5)
v

[1] -2.078349536  0.004373942  0.269364879  0.199779240  0.373235666

ind <- which(v < -0.5 | v > 0.5)
v[ind] <- v[ind] * 2
v

[1] -4.156699071  0.004373942  0.269364879  0.199779240  0.373235666

Maybe a function like this would make sense:

也许像这样的函数是有道理的:

twiddle <- function(v, multiplier = 2) {
  ind <- which(v < -0.5 | v > 0.5)
  v[ind] <- v[ind] * multiplier
  return(v)
}

Or more compactly, just v[abs(v) > 0.5] <- v[abs(v) > 0.5] * multiplier; return(v) as pointed out in the comment.

或者更紧凑,只是v [abs(v)> 0.5] < - v [abs(v)> 0.5] *乘数;返回(v),如评论中所指出。

Called like this:

这样称呼:

u <- rnorm(1000)
twiddle(u)

#2


2  

You can try the following codes which ask for an argument of the number of samples to be generated:

您可以尝试以下代码,询问要生成的样本数量的参数:

my.func = function(n=1000){
  rn = rnorm(n)
  good = rn[rn > 0.5 | rn < -0.5]
  return(2*good)
}

Run my.func(1000) to get the vector.

运行my.func(1000)以获取向量。

#1


3  

Using a loop in this way is not idiomatic R. R is a vectorized language which means loops are not needed to do arithmetic on vectors. I recommend reading An Introduction to R to get familiar with the standard approaches.

以这种方式使用循环不是惯用的R.R是一种矢量化语言,这意味着不需要循环来对矢量进行算术运算。我建议阅读R简介以熟悉标准方法。

Also I don't think it's optimal to compute your random vector inside the function (it will make it hard to debug); maybe you want to pass it as an argument.

另外我认为在函数内部计算随机向量并不是最优的(它会使调试变得困难);也许你想把它作为一个参数传递。

The following code will do what you want:

以下代码将执行您想要的操作:

v <- rnorm(5)
v

[1] -2.078349536  0.004373942  0.269364879  0.199779240  0.373235666

ind <- which(v < -0.5 | v > 0.5)
v[ind] <- v[ind] * 2
v

[1] -4.156699071  0.004373942  0.269364879  0.199779240  0.373235666

Maybe a function like this would make sense:

也许像这样的函数是有道理的:

twiddle <- function(v, multiplier = 2) {
  ind <- which(v < -0.5 | v > 0.5)
  v[ind] <- v[ind] * multiplier
  return(v)
}

Or more compactly, just v[abs(v) > 0.5] <- v[abs(v) > 0.5] * multiplier; return(v) as pointed out in the comment.

或者更紧凑,只是v [abs(v)> 0.5] < - v [abs(v)> 0.5] *乘数;返回(v),如评论中所指出。

Called like this:

这样称呼:

u <- rnorm(1000)
twiddle(u)

#2


2  

You can try the following codes which ask for an argument of the number of samples to be generated:

您可以尝试以下代码,询问要生成的样本数量的参数:

my.func = function(n=1000){
  rn = rnorm(n)
  good = rn[rn > 0.5 | rn < -0.5]
  return(2*good)
}

Run my.func(1000) to get the vector.

运行my.func(1000)以获取向量。