找到一个与R中的min函数相同的函数

问题描述 投票:0回答:1

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

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

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

}

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

r function
1个回答
-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
© www.soinside.com 2019 - 2024. All rights reserved.