在R中找到与min函数相同的函数

时间:2019-03-11 18:58:23

标签: r function

我正在努力寻找一个仅通过使用Rstudio中的命令(if,else,for,while)来查找列表最小值的函数。我尝试这样做:

for (i in seq_along(x[  i  ])){

if (x[  i  ] < x[  i+1  ]) {print(x[  i  ])}

}

但是它不起作用。你能帮我吗?

1 个答案:

答案 0 :(得分:-1)

#DATA
set.seed(42)
x = sample(1:10)
x

# Create a variable to store the minimum value.
#Initially, set its value to be the first element of 'x'
minvalue = x[1]

for(i in seq_along(x)){  #Your code is wrong here
    #Write the code here yourself. You want to test if the current element of 'x' (x[i])
    #is smaller than 'minvalue'. If it is, the new value of 'minvalue' will be the current
    #element of 'x'
}
minvalue