Lecture 03

1. Last Lecture - List

> x = matrix(1:20, nrow=5, ncol=4, byrow=TRUE)
> swim = read.csv("http://www.macalester.edu/~kaplan/ISM/datasets/swim100m.csv")
> mylist=list(swim,x)    # 創(chuàng)建mylist,包含swim和x

> mylist[2]
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20

> mylist[[2]][2]    #返回mylist第2部分的第2個元素(默認豎向)
[1] 5
> mylist[[2]][3]
[1] 9

> mylist[[2]][1:2]    #返回mylist第2部分的前2個元素
[1] 1 5
> mylist[[2]][1:2,3]    #1,2兩行,第3列
[1] 3 7
> mylist[[2]][1:2,]    #1,2兩行
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8

2. Operators

( "three value logic"-三值邏輯 )

>, >=, <, <=, ==, !=(不等于), x | y(或), x & y(且)

3. Control Flow

3.1 Repetition and looping

Looping constructs repetitively execute a statement or series of statements until a condition is not true. These include the for and while structures.

> #For-Loop
> for(i in 1:10){      # 1到10
+   print(i)
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

> #While-Loop
> i = 1
> while(i <= 10){      # 1到10
+   print(i)
+   i=i+1
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
3.2 Conditional Execution

In conditional execution, a statement or statements are only executed if a specified condition is met. These constructs include if, if-else, and switch.

> #If statement
> i = 1
> if(i == 1){
+   print("Hello World")
+ }
[1] "Hello World"

> #If-else statement
> i = 2
> if(i == 1){
+   print("Hello World!")
+ }else{
+   print("Goodbye World!")
+ }
[1] "Goodbye World!"

> #switch
> #switch(expression, cnoditions)
> feelings = c("sad", "afraid")
> for (i in feelings){
+   print(
+     switch(i,
+            happy  = "I am glad you are happy",
+            afraid = "There is nothing to fear",
+            sad    = "Cheer up",
+            angry  = "Calm down now"
+     )
+   )
+ }
[1] "Cheer up"
[1] "There is nothing to fear"

4. User-defined Function

One of greatest strengths of R is the ability to add functions. In fact, many of the functions in R are functions of existing functions.

> myfunction = function(x,a,b,c){
+ return(a*sin(x)^2 - b*x + c)
+ }
> curve(myfunction(x,20,3,4),xlim=c(1,20))
> curve(exp(x),xlim=c(1,20))
> myfeeling = function(x){
+   for (i in x){
+     print(
+       switch(i,
+              happy  = "I am glad you are happy",
+              afraid = "There is nothing to fear",
+              sad    = "Cheer up",
+              angry  = "Calm down now"
+       )
+     )
+   }
+ }
> feelings = c("sad", "afraid")
> myfeeling(feelings)
[1] "Cheer up"
[1] "There is nothing to fear"
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 大數據和高性能計算基礎 非并行I/O 上面每一個方塊代表不同的進程,非并行是每一個進程通過通信的方式傳給某一個進程...
    全村滴希望閱讀 277評論 0 0
  • 本文轉載自知乎 作者:季子烏 筆記版權歸筆記作者所有 其中英文語句取自:英語流利說-懂你英語 ——————————...
    Danny_Edward閱讀 44,070評論 4 38
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,893評論 0 13
  • 最近重新追星的我真的是酸透了! 據傳言有人說吳世勛連續(xù)兩次逗得那個女生是他的理想型,啊啊啊啊啊好酸啊我! 太羨慕了...
    小高瑞呀閱讀 75評論 0 0
  • 序 很多時候我們會用Docker進行部署,其實它還可以用于開發(fā)。 為什么要在開發(fā)中使用Docker? 主要有以下幾...
    技術學習閱讀 8,479評論 0 20

友情鏈接更多精彩內容