print("x")
"Control Structure"
if, else:testing a condition
for:execute a loop a fixed number of times
while:execute a loop while a condition is true
repeat:execute an infinite loop
break:break the execution of a loop
next:skip an interation of a loop
return:exit a function
"判断"
x<-2
if(x>3){
y<-10
}else{
y<-0
}
other way
y<- if(x>3 ){
10
}else{
0
}
"循环"
for(i in 1:10){
print(i)
}
x<-c("a","b","c","d")
for(i in 1:4){
print(x[i])
}
for(i in seq_along(x)){
print(x)#是个向量
}
for(letter in x){
print(letter)
}
for(i in 1:4)print(x[i])
x <- matrix(1:6, 2, 3)
for(i in seq_len(nrow(x))){
for(j in seq_len(ncol(x))){
print(x[i, j])
}
}# 先row 行
"while 循环"
count <- 0
while(count < 10){
print(count)
count<- count + 1
}
count<- 0
z<- 5
while(z >= 3 && z<= 10){
print(z)
coin <- rbinom(1,1,0.5)
count <- count +1
if(coin ==1){
z<- z+1
}else{
z<- z-1
}
print(count)
}
"repaet "
x0 <- 1
tol <- 1e-8
repeat{
x1<- computeEstimate()#这里是估计值
if(abs(x1-x0)< tol){
break
}else{
x0 <- x1
}
}
break 直接跳出
next 直接跳过
"function"
add2 <- function(x,y){
x+y
}
above10 <- function(x){
use<- x>10
x[use]
}
above <- function(x,n = 10){
use<- x > n
x[use]
}# n 的defeat值为10
columnmean <- function(y){
nc <- ncol(y)
means <- numeric(nc)
for(i in 1:nc){
means[i] <- mean(y[, i])
}
means
}
#存在NA值 处理NA值
columnmean <- function(y, removeNA= TRUE){
nc <- ncol(y)
means <- numeric(nc)
for(i in 1:nc){
means[i] <- mean(y[, i], na.rm = removeNA)
}
means
}
# 函数可以嵌套
# 顺序排列和非顺序排列都可以,没有命运就看着顺序赋值
#建议顺序赋值参数
mydata <- rorm(100)
sd(mydata)
sd(x = mydata)
sd(x = mydata, na.rm = FALSE)
sd(na.rm = FALSE, x = mydata)
sd(na.rm = FALSE, mydata)
args(lm)
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
lm(data = mydata, y-x, model = FALSE, 1:100)
lm(x-y, mydata, 1:100, model = FALSE)
#后两者都是一样的,可以理解位置匹配和命名匹配是如何协调的
f <- function(a, b = 1, c = 2, d=NULL){
}
#参数可以设置成null
f <- function(a, b){
a^2
}
f(2)
f <- function(a, b){
print(a)
print(b)
}
f(45)
#有些参数在参数的里面
function (x, ...)
UseMethod("mean")
<bytecode: 0x000001c3cbdb34a8>
<environment: namespace:base>
args(paste)
function (..., sep = " ", collapse = NULL, recycle0 = FALSE)
NULL
> paste("a","b",sep = ":")
[1] "a:b"
> paste("a","b",se = ":")
[1] "a b :"
"全局变量"
#R的包存在一个固定的包列表或是这些包以某种特定顺序排列
#library()这个包就会在第二个被搜索的,也就是全局环境的后面
#可能有一个叫c的对象和一个叫c的函数
# 作用域
f <- function(x,y){
x^2 + y/z
}#作用域决定了z
#base包是最后搜索的
#函数里面构建函数
make.power <- function(n){
pow <- function(x){
x^n
}
}
cube <- make.power(3)
square <- make.power(2)
cube(3)
square(3)
> cube <- make.power(3)
> square <- make.power(2)
> cube(3)
[1] 27
>
> square(3)
[1] 9
> ls(environment(cube))
[1] "n" "pow"
> get("n",environment(cube))
[1] 3
> ls(environment(square))
[1] "n" "pow"
> get("n",environment(square))
[1] 2
# 看起来也是先读取全部的函数
y <- 10
f<- function(x){
y <- 2
y^2 + g(x)
}
g <- function(x){
x*y
}
f(3)
#要是y放在后面则不能识别出来
make.NegLogLik <- function(data, fixed = c(FALSE,FALSE)){
params <- fixed
function(p){
params[!fixed] <- p
mu <- params[1]
sigma <- params[2]
a <- 0.5*length(data)*log(2*pi*sigma^2)
b <- 0.5*sum((data-mu)^2)/(sigma^2)
-(a+b)
}
}
"代码的规则"
1、用文本编辑器
2、缩进至少四个代码
3、限制长度
4、限制函数功能(debug能找到函数的)
"日期"
> x <- as.Date("1970-01-01")
> x
[1] "1970-01-01"
> unclass(x)
[1] 0
> unclass(as.Date("1970-01-01"))
[1] 0
POSIXct整数
POSIXlt 是个列表
> x <- Sys.time()
> x
[1] "2022-10-01 12:02:56 CST"
> p<- as.POSIXlt(x)
> names(unclass(p))
[1] "sec" "min" "hour" "mday" "mon" "year"
[7] "wday" "yday" "isdst" "zone" "gmtoff"
> p$sec
[1] 56.42621 #当前时间的秒数
datastring <- c("January 10, 2012 10:40","December 9, 2011 9:10")
x <- strptime(datastring, "%B %d, %Y %H:%M")
x
class(x)
#不同类型的不能相加减
x <- as.Date("2012-03-01")
y<- as.Date("2012-02-28")
x-y
x<- as.POSIXct("2012-10-25 01:00:00")
y<- as.POSIXct("2012-10-25 06:00:00",tz="GMT")
x-y
R学习——霍普金斯大学week2
最新推荐文章于 2025-01-16 14:57:06 发布
170

被折叠的 条评论
为什么被折叠?



