R语言中级--自定义方程
生活随笔
收集整理的這篇文章主要介紹了
R语言中级--自定义方程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#求電費(fèi),<50,85折,50--120,50元/度,>120,1.15倍
myfunction <- function(deg,price = 50){if(deg>120)energyprice = deg*price*1.15else if(deg<80)energyprice = deg*price*0.85else energyprice = deg*pricereturn(round(energyprice))}
myfunction(100)myfunction2 <- function(deg,price = 50, poor = FALSE)
{energyprice = deg*price
if (deg > 100)energyprice = deg*price*1.15
elseif (poor == TRUE)energyprice = energyprice*0.85*0.7elseenergyprice = energyprice*0.85return(round(energyprice))}
myfunction2(80,poor = TRUE)#給定一個數(shù)x,求x*(x-1)*...*2*1
myfunction3 <- function(x)
{ if (x == 0)x_sum = 1
else x_sum = x*myfunction3(x-1)
return(x_sum)}
myfunction3(4)ifelse(condition,statement1,statement2)#求1到n 之和
myfunction4 <- function(n)
{sum = 0
for (i in 1:n)sum = sum + i
return(sum)}
myfunction4(100)
sum(1:100)#同fun4
myfunction5 <- function(n)
{sum = 0
while (n >= 0) { sum = sum + nn = n - 1}
return(sum)
}
myfunction5(100)#同fun4
myfunction6 <- function(n)
{sum = 0
repeat{ sum = sum + nn = n - 1if(n == 0) break
}
return(sum)}
myfunction6(100)#奇數(shù)之和
myfunction7<- function(n)
{sum=0
for(i in 1:n)
{if(i %%2 !=0) next sum=sum+i
}
return(sum)
}
myfunction7(100)
x<- c(1:100)
x %% 2
轉(zhuǎn)載于:https://www.cnblogs.com/super-yb/p/11047938.html
總結(jié)
以上是生活随笔為你收集整理的R语言中级--自定义方程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS基础学习-15-1.CSS 浏览器
- 下一篇: 在java web工程中jsp页面中使用