将答案从函数存储到R中的向量中 [英] Storing Answers From Functions into a Vector in R
问题描述
系统提示我一个问题,而且离解决我所需要的一切都非常接近.问题如下-
编写一个while循环,该循环将在每次支撑代码重复时将mynum减1,从而将任何非负整数mynum的阶乘计算并存储为新对象.
另一个因素是,如果输入0或1,则输出将为1.
我编写的代码如下-
factorialcalc<-函数(i){阶乘<-1if(i == 0 | i == 1){阶乘<-1} 别的{while(i> = 1){阶乘<-阶乘* i我<-i-1}}回报(因子)}
带有输入-
mynum<-5factorialcalc(mynum)
并输出-
[1] 120
您可能想知道,您的代码可以完美运行,所以出了什么问题?"我的问题在于问题的一部分,即计算并存储".
如何修改我的代码以将factorialcalc的答案放入向量中?
示例-我输入
mynum<-5factorialcalc(mynum)
和
mynum<-3factorialcalc(mynum)
和
mynum<-4factorialcalc(mynum)
当我调用这个新矢量时,我想看到一个带有所有三个输出的矢量(所以几乎就像我制作了一个向量c(120,6,24))
我在想有一种方法可以将此向量添加到函数或while循环中的某个位置,但是我不确定在哪里.另外,请注意,答案必须包含一个循环,就像我的代码中一样.
选项1.
矢量化";您的功能
#只需将整个内容包装在Vectorize()中Factorialcalc = Vectorize(function(i){阶乘<-1if(i == 0 | i == 1){阶乘<-1} 别的{while(i> = 1){阶乘<-阶乘* i我<-i-1}}回报(因子)})#现在,当您提供向量时,它会在每个元素上运行>Factorialcalc(c(5,3,4))[1] 120 6 24
选项2.
使用旨在将单个功能应用于所提供矢量的多个元素的功能.使用 purrr
包中的 map_dbl
,您可以调用:
map_dbl(c(5,3,4),factorialcalc)
哪个函数将 vector
中的每个元素提供给函数 factorialcalc
,并在返回矢量之前将每个结果连接起来.
使用 base R
,您可以简单地使用 apply
-family函数:
sapply(c(5,3,4),factorialcalc)
并获得相同的结果.
示例
>map_dbl(c(5,3,4),factorialcalc)[1] 120 6 24>sapply(c(5,3,4),factorialcalc)[1] 120 6 24
I was prompted a question and am ever so close to solving what I need. The question is as follows-
"Write a while loop that computes and stores as a new object, the factorial of any non-negative integer mynum by decrementing mynum by 1 at each repetition of the braced code."
Another factor was that if 0 or 1 was entered, the output would be 1.
The code that I wrote as follows-
factorialcalc <- function(i){
factorial <- 1
if(i==0 | i==1){
factorial <- 1
} else{
while(i >= 1){
factorial <- factorial * i
i <- i-1
}
}
return (factorial)
}
with inputs-
mynum <- 5
factorialcalc(mynum)
and output-
[1] 120
You may be wondering, "your code works perfect, so what's the issue?" My issue lies in the part of the question that says "computes AND stores."
How can I modify my code to put the answers of factorialcalc into a vector?
Example- I input
mynum <- 5
factorialcalc(mynum)
and
mynum <- 3
factorialcalc(mynum)
and
mynum <- 4
factorialcalc(mynum)
When I call this new vector, I would like to see a vector with all three of their outputs (so almost like I made a vector c(120,6,24))
I'm thinking there's a way to add this vector somewhere in my function or while loop, but I'm not sure where. Also, please note that the answer must contain a loop like in my code.
Option 1.
"Vectorize" your function
# simply wrap the whole thing in Vectorize()
Factorialcalc = Vectorize(function(i){
factorial <- 1
if(i==0 | i==1){
factorial <- 1
} else{
while(i >= 1){
factorial <- factorial * i
i <- i-1
}
}
return (factorial)
})
# Now when you supply it a vector, it runs on each element
> Factorialcalc(c(5, 3, 4))
[1] 120 6 24
Option 2.
Use functions that are designed to apply a single function to multiple elements of a supplied vector.
Using map_dbl
from the purrr
package, you can call:
map_dbl(c(5, 3, 4), factorialcalc)
Which supplies to your function factorialcalc
each element in vector
and concatenates each result before returning a vector.
Using base R
you can simply use the apply
-family functions:
sapply(c(5, 3, 4), factorialcalc)
and get the same result.
Example
> map_dbl(c(5, 3, 4), factorialcalc)
[1] 120 6 24
> sapply(c(5, 3, 4), factorialcalc)
[1] 120 6 24
这篇关于将答案从函数存储到R中的向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!