gpt4 book ai didi

r - 如何在 r 中将函数数据更改为向量

转载 作者:行者123 更新时间:2023-12-05 02:25:58 25 4
gpt4 key购买 nike

xmin = as.integer(readline("Input the left boundary of the period: "))
xmax = readline("Input the right boundary of the period: ")
step = readline("Input the step of the sequence: ")

new.function = function(xmin, xmax, step) {
while (xmin<=xmax) {
y = (5 + log(xmin) * exp(xmin) * xmin - (1.5/xmin))
print(y)
xmin = as.integer(xmin) + as.integer(step)
}
}


v <- unlist(new.function(xmin, xmax, step))
print(v)

我想将答案改为向量,这意味着在一行中显示所有答案不在连续的 4 行中。如果我输入 1、4、1,答案将从上到下为 3.5 到 307.1。是否可以一行得到答案? (3.5, 14.49341, 70.69865, 307.3814)

最佳答案

将结果存储在向量中而不是打印它们:

new.function = function(xmin, xmax, step) {
y <- c() ## Add this to your function
while (xmin<=xmax) {
y = c(y, (5 + log(xmin) * exp(xmin) * xmin - (1.5/xmin))) # Modify this
xmin = as.integer(xmin) + as.integer(step)
}
y
}


v <- new.function(xmin, xmax, step)
v

关于r - 如何在 r 中将函数数据更改为向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74133306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com