[英]Unable to resolve a simple If-Else Function error in R
I am just trying to gain some knowledge about how to write functions in R. I created a simple If-else
function which basically takes numerical input in function call. 我只是想获得一些有关如何在R中编写函数的知识。我创建了一个简单的
If-else
函数,该函数基本上在函数调用中接受数字输入。
If user gives n=1
, iris dataframe is to be created and for any other number mtcars
. 如果用户给出
n=1
, mtcars
创建虹膜数据帧,并创建其他任何数目的mtcars
。
df1<-iris
df2<-mtcars
run<-function(n){
if(n==1){
data1<-df1
} else {
data1<-df2
}
}
run(1)
But somehow this is just not working. 但是以某种方式这是行不通的。 I am missing something here.
我在这里错过了一些东西。
What @C_Z_ said @C_Z_说了什么
df1<-iris
df2<-mtcars
run<-function(n, df1, df2){
if(n==1){
data1<-df1
} else {
data1<-df2
}
return(data1)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.