mean(1:3)
1:3
1:5
6:10
1:5+6:10
C(1,3,4,7,8) - 2
1:10%/%3
factorial(5)
-2:2 * -2:2
10%%3
c(1,3,4-1,2+1) ==3
1:3 != 3:1
(1:5)^2 >= 16
sqrt(2)^2 == 2
x<<- sqrt(4)
x
assign("ma_local_variable", 9^3+10^3)
ma_local_variable
x1 = c(1/Inf, Inf/1, Inf/Inf)
x1
x2 = c(NA+1, NA*5, NA+Inf)
x2
(x <- 1:10 >= 5)
(y <- 1:10 %% 2 ==0)
x&y
x|y
none_true <- c(FALSE, FALSE, FALSE)
some_true <- c(FALSE, TRUE, FALSE)
all_true <- c(TRUE, TRUE, TRUE)
any(none_true)
any(some_true)
all(none_true)
all(all_true)
class(1+3i)
class(1L)
gender <- factor(c("female","male"))
gender
class(gender)
levels(gender)
nlevels(gender)
as.integer(gender)
#检查和更改类
if(is(x,"ss")){
}
is.character("red lolly")
is.logical(FALSE)
is.list(list(a=1,n=2))
ls(pattern="^is",baseenv())
as.numeric(x)
x <- "123.456"
class(x) <- "numeric"
x
#检查变量或工作区
1+1+1
print(1+1+1)
ulams_spiral <- c(1,8,23,46,77)
for(i in ulams_spiral) i
for(i in ulams_spiral) print(i)
num <- runif(30) #生成随机30个0~1之间的数
num
summary(num)
str(num)
?str
fac <- factor(sample(letters[1:5],30,replace = TRUE))
unclass(fac)
?sample
attributes(fac)
ls()
peach <- 1
plum <- "fruity"
pear <- TRUE
ls()
ls(pattern = "ea")
rm(peach,plum,pear)
# rm(list=ls())
#### 向量 矩阵
c(1:3,c(5:8),13)
vector("numeric",5)
vector("complex",5)
vector("logical",5)
vector("character",5)
numeric(5)
complex(5)
logical(5)
character(5)
seq.int(3,12)
seq.int(3,12,2)
seq_len(5)
pp <- c("fds","fdsfgg","fgserf")
seq_along(pp)
for(i in seq_along(pp)) print(pp[i])
length(1:5)
length(c(TRUE,FALSE,NA))
length(sn)
c(apple=1,banana=2,"kivifruit"=3,4)
x<-1:4
names(x)<-c("apple","banana","kiwifruit")
x
### 索引向量
x<-(1:5)^2
x
x[c(1,3,5)]
x[c(-2,-4)]
x[c(TRUE,FALSE,TRUE,FALSE,TRUE)]
x<-c("one","two","three")
x[]
1:5+1
1+1:5
1:5+1:15
rep(1:5,3)
rep(1:5,each=3)
rep_len(1:5,13)
# 矩阵
a_matrix <- matrix(1:12,nrow=4,dimnames=list(
c("one","two","three","four"),
c("ein","zwei","drei")
))
a_matrix
# 二维数组
two_d_arrary<-array(1:12,dim=c(4,3),
dimnames=list(
c("one","two","three","four"),
c("ein","zwei","drei")))
two_d_arrary
dim(two_d_arrary)
dim(a_matrix)
length(two_d_arrary)
length(a_matrix)
rownames(a_matrix)
rownames(two_d_arrary)
colnames(a_matrix)
colnames(two_d_arrary)
a_matrix[1,c("zwei","drei")]
a_matrix[,c("zwei","drei")]
a_matrix[2,]
another_matrix<-matrix(seq.int(2,24,2),
nrow=4,
dimnames=list(
c("five","six","seven","eight"),
c("vier","funf","sechs")
))
another_matrix
if(TRUE) message("it was true")
if(FALSE) messgae("it wasn't true")
x<-3
if(x>2){
y<-2*x
z<-3*y
}
y
z
if(FALSE) {
message("this won't be execute")
}else{
message("but this will")
}
yn<-rep.int(c(TRUE,FALSE),6)
yn
ifelse(yn,1:3,-1:-12)
if(c(0)) message("hello")
greek<-switch("gama",
alpha=1,
beta=sqrt(4),
gama={
a<-sin(pi/3)
4*a^2
})
greek
greek<-switch("gama",
alpha=1,
beta=sqrt(4),
gama2={
a<-sin(pi/3)
4*a^2
})
greek
repeat{
message("Happy groundhog day!")
action<-sample(c(
"learn French",
"make an ice statue",
"ROb a bank",
"win heart of Andie Mcdowell"
),1)
message("action=",action)
if(action == "win heart of Andie Mcdowell")
break
}
for (i in 1:5) message("i=",i)
for(i in 1:5){
j<-i^2
message("j=",j)
}
for(month in month.name){
message(month)
}
for(yn in c(TRUE,FALSE,NA)){
message("This statement is ", yn)
}
l<-list(pi,LETTERS[1:5],charToRaw("not as complicated as it
looks"))
for(i in l){
print(i)
}
an_env<-new.env()
an_env
an_env[["pythag"]] <- c(12,15,20,21)
an_env$pythag
an_env$root <- polyroot(c(6,-5,1))
an_env$root
?polyroot
assign("monday",weekdays(as.Date("1969-07-20")),an_env)
an_env[["pythag"]]
an_env$root
get("monday",an_env)
nested_env<-new.env(parent=an_env)
exists("pythag",nested_env)
exists("pythag",nested_env,inherits=FALSE)
non_stormers<<-c(3,7,8,13,17,18,21)
get("non_stormers",envir=globalenv())
twosam <- function(y1,y2){
n1 <- length(y1); n2<- length(y2)
yb1 <- mean(y1); yb2 <- mean(y2)
s1 <- var(y1); s2<- var(y2)
s <- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2)
tst <- (yb1 - yb2)/sqrt(s*(1/n1 + 1/n2))
print(yb1)
print(yb2)
print(s1)
print(s2)
print(s)
print(tst)
}
twosam(c(1:3),c(4:6))
R语言语法和Tips
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- !!(x) :转换x为bool值,x为0则返回0,不为0则返回1。 offsetof宏使用offsetof宏需要包...
- 学R的时候没有做思维导图,只是列出目前已学的R语言语法,方便自己复制==,语法包括如下: 建立Rstudy为工作路...
- 1:C语言不存在boolean的值,是0或1,虽然可以声明bool但是底层仍旧是0或1;Java存在boolean...