我正在努力找到一个函数,它只通过使用Rstudio中的命令(if,else,for,while)来查找列表的最小值。我试着这样做:
for (i in seq_along(x[ i ])){
if (x[ i ] < x[ i+1 ]) {print(x[ i ])}
}
但它不起作用。你能帮助我吗 ?
#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