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)    # 创建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"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 大数据和高性能计算基础 非并行I/O 上面每一个方块代表不同的进程,非并行是每一个进程通过通信的方式传给某一个进程...
    全村滴希望阅读 1,756评论 0 0
  • 本文转载自知乎 作者:季子乌 笔记版权归笔记作者所有 其中英文语句取自:英语流利说-懂你英语 ——————————...
    Danny_Edward阅读 44,028评论 4 38
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,229评论 0 13
  • 最近重新追星的我真的是酸透了! 据传言有人说吴世勋连续两次逗得那个女生是他的理想型,啊啊啊啊啊好酸啊我! 太羡慕了...
    小高瑞呀阅读 619评论 0 0
  • 序 很多时候我们会用Docker进行部署,其实它还可以用于开发。 为什么要在开发中使用Docker? 主要有以下几...
    技术学习阅读 12,631评论 0 20

友情链接更多精彩内容