Week 1 Tutor -- R语言入门(2)-- 函数


Reading/Writing Data

读取的文件在你的当前工作文件夹下 查看当前路径/查看当前路径文件 getwd() / dir() 读取数据 data <- read.table(file='airfoil_self_noise.txt') 写数据 write.csv(data, file="mydata.csv") 排序 sort(x, decreasing = FALSE)

逻辑运算

和运算 7>1 & 1<7 或运算 1>2 | 2>1 备注:&&和||只比较左边和右边的第一个元素,而&和|会比较所有的 If-Else函数 x <- seq(10) ifelse(x %% 2 == 0,"even","odd")

Control Structures

IF结构 if(x == 2){ print("Yesss") } For结构 for (i in 1:5){ print(i) } While结构 while(count >0){ print(count) count <- count -1 } Any()和 all()结构 if (any(x > 12)) print("No Way!") if (all(x > 7)) print("Another one!") 自定义函数 myfunc1 <- function(n){ n*n Call自定义函数 myfunc1(5) 随机函数:sample()从指定的一组(标量)对象中随机抽取 sample(1:10,4) ## [1] 3 9 8 5