R随笔(1)

R随笔(1)

nested_list<-list(m=1:15,n=list("a",c(1,2,3)),p=list(x=1:10,y=c("a","b")),q=list(x=0:9,y=c("c","d")))
str(nested_list)
summary(nested_list)
dim(nested_list)
cat(nested_list)
ls.str(nested_list)
ls.str(mode="list")
rm(z)
ls()
rm(nested_list,m)
getOption(<Tab>)
getOption("digits")
search()
sessionInfo()
##########
grade2<-function(score){
 if(score>=60&&score<70){
  return("d")
 }else if(score>=70&&score<80){
  return("c")
 }else if(score>=80&&score<90){
  return("b")
 }else{
  return("a")
 }
}
##############
x<-0
while(TRUE){
 x<-x+1
 if(x==4) break
 else if(x==2) next
 else cat (x,'\n')
}
###
x<-0
repeat{
 x<-x+1
 if(x==4) break
 else if(x==2) next
 else cat (x,'\n')
}
###
take_it<-function(x){
 if(is.atomic(x)){
  x[[1]]
 }else if(is.list(x)){
  x$data[[x$index]]
 }else{
  stop("not supported input type")
 }
}
###
#识别数据的类型
take_it(c(1,2,3))
take_it(list(data=c("a","b","c"),index=3))
class(x)
typeof(x)
typeof(y)
str(x)
class(x)
class(nested_list)
str(nested_list)
###
#识别数据维度
p<-c(1:12)
q<-matrix(p,ncol=4)
dim(q)
View(q)
nrow(q)
ncol(q)
class(q)
typeof(q)
q1<-array(p,dim=c(2,3,2))
View(q1)
View(p)
sample_data<-p
dim(sample_data)<-c(3,4)
View(sample_data)
class(sample_data)
typeof(sample_data)
dim(sample_data)<-c(2,6)
View(sample_data)
class(sample_data)
dim(sample_data)<-c(2,3,2)
View(sample_data)
###
test_direction<-function(x,y,z){
 if(x<y&&y<z){
  1
 }else if(x>y&&y>z){
  -1
 }else{
  0
 }
}
test_direction(1,2,3)
test_direction(9,8,7)
test_direction(c(1,2),c(2,3),c(3,4))
###
x<-c(-2,-1,0,1,2,3)
any(x>1)
any(x!=9)
any(x==9)
all(x>9)
all(x!=9)
###
test_all_direction<-function(x,y,z){
 if(all(x<y&&y<z)){
  1
 }else if(all(x>y&&y>z)){
  -1
 }else{
  0
 }
}
test_all_direction(1,2,3)
test_all_direction(78,23,54)
test_all_direction(c(1,2),c(2,3),c(3,3))
###
test_all_direction2<-function(x,y,z){
 if(all(x<y)&&(y<z))1
 else if(all(z>y)&&(y>z))-1
 else 0
}
ls()
search()
x
x>=2
x!=2
which(x>1)
###
pi
ceiling(pi)
floor(pi)
trunc(pi)
round(pi)
signif(pi)
sin(pi)
cos(pi)
tan(pi)
asin(pi)
acos(pi)
atan(pi)
sinh(pi)
cosh(pi)
tanh(pi)
asinh(pi)
acosh(pi)
atanh(pi)
max(x)
min(x)
###
spread<-function(x){
 if(x<-5) -5
 else if(x>5) 5
 else x
}
spread(5)
spread(4)
###
spread2<-function(x){
 pmin(5,pmax(-5,x))
}
spread2(seq(-8,8))
###
uniroot(function(x)^2-exp(x),c(-2,1))
###
D(quote(x^2),"x")
D(quote(sin(x)*cos(x*y)),"x")
z<-D(quote(sin(x)*cos(x*y)),"x")
z
eval(z,list(x=1,y=4))
result<-integrate(function(x) sin(x),0,pi/2)
result
str(result)
sample(1:6,size=5)
sample(1:6,size=5,replace = TRUE)
sample(letters,size=3)
grades<-sample(c("A","B","C"),size=20,replace = TRUE,prob=c(0.25,0.5,0.25))
grades
table(grades)
###
runif(10,min=6,max=10000)
qplot(x,y)
rnorm(5)
rnorm(5,mean=2,sd=0.5)
x<-rnorm(50)
x
mean(x)
sum(x)/length(x)
sd(x)
mean(x,trim=0.05)
x
median(x)
sd(x)
var(x)
range(x)
min(x)
max(x)
summary(x)
class(x)
cov(x,y)
y<-2*x+0.5*rnorm(length(x))
cor(x,y)
###
len<-c(3,4,5)
x<-list()
for (i in 1:3){
 x[[i]] <-rnorm(len[i])
}
x
###
grade<-function(score){
 if(score>=90){
  return("A")
 }else if(score>=80){
  return("B")
 }else if(score>=70){
  return("C")
 }else if(score>=60){
  return("D")
 }else{
  cat("gun...")
 }
}
grade(66)
grade(23)
###
grade2<-function(score){
 if(score>=60&&score<70){
  return("D")
 }else if(score>=70&&score<80){
  return("C")
 }else if(score>=80&&score<90){
  return("B")
 }else if(score>=90&&score<=100{
  return("C")
 }else{
  return("gun...")
 }
}
###
grade3<-function(score){
 if(score>=90&&score<=100){
  return("A")
 }else if(score>=80&&score<90){
  return("B")
 }else if(score>=70&&score<80){
  return("C")
 }else if(score>=60&&score<70){
  return("D")
 }else{
  return("E")
 }
}
grade3(90)
grade3(77)
grade3(66)
grade3(23)
###if表达式
say_grade<-function(name,score){
 grade<-if(score>=90) "A"
 else if(score>=80) "B"
 else if(score>=70) "C"
 else if(score>=60) "D"
 else "E"
 cat("the grade of",name,grade)
}
say_grade("zhangsan",98)
say_grade("lisi",78)
###
ifelse(c(TRUE,FALSE,FALSE),c(1,2,3),c(4,5,6))
###
switch()
switch (1,"x","y")
switch(2,"x","y")
###
switch_test<-function(x){
 switch(x,
        a=c(1,2,3),
        b=list(x=0,y=1),
        c={cat("you choose c!\n")
         list(name="c",value="something")
         })
}
switch_test ("a")
switch_test("b")
switch_test("c")
###
install.packages("tidyverse")
?qplot()
set.seed(1410)
dsmall<-diamonds[sample(nrow(diamonds),100),]
library(ggplot2)
qplot(carat,price,data=dsmall)
qplot(carat,price,data=diamonds)
qplot(log(carat),log(price),data=diamonds)
qplot(carat,x*y*z,data=diamonds)
qplot(carat,price,data=dsmall,colour=color)
qplot(carat,price,data=diamonds,colour=factor("price"))
qplot(carat,price,data=dsmall,shape=cut)
qplot(carat,price,data=diamonds,shape=cut,colour=color)
p<-qplot(carat,price,data=diamonds,shape=cut,colour=color)
getwd()
setwd("c:/Users/Smyang2018/Desktop/")
qplot(carat,price,data=diamonds,shape=clarity,colour=factor("x"))
qplot(carat,price,data=dsmall,shape=clarity,colour=I("green"),size=I(0.5))
qplot(carat,price,data=diamonds,shape=cut,colour=factor("cut"),size=I(1/2))
qplot(carat,price,data=diamonds,alpha=I(1/10))
qplot(carat,price,data=diamonds,alpha=I(1/10),colour=I("red"),size=I(1/2))
qplot(carat,price,data=diamonds,alpha=I(1/50),colour=I("green"),size=I(2/3))
qplot(carat,price,data=diamonds,alpha=I(1/100),colour=I("red"),size=I(5/8),shape=cut)
qplot(carat,price,data=dsmall,geom=c("point","smooth"))
qplot(carat,price,data=dsmall,geom=c("point","smooth"),alpha=I(1/10),colour=I("red"),size=I(1/2),shape=cut)
qplot(carat,price,data=diamonds,geom=c("point","smooth"),alpha=I(1/10),colour=I("red"),size=I(1/2),shape=cut,xlab="who are you",ylab="sm",main="the graphical of write")
?smooth
smooth(x, kind = c("3RS3R", "3RSS", "3RSR", "3R", "3", "S"),
       twiceit = FALSE, endrule = c("Tukey", "copy"), do.ends = FALSE)
?loess
qplot(carat,price,data=dsmall,geom=c("point","smooth"),span=0.2)
qplot(carat,price,data=diamonds,geom=c("point","smooth"),span=1)
library(mgcv)
qplot(carat,price,data=diamonds,geom=c("point","smooth"),method="gam",formula=y ~s(x))
qplot(carat,price,data=dsmall,geom=c("point","smooth"),method="gam",formula=y~s(x,bs="cs"))
??method
qplot(carat,price,data=diamonds,geom=c("point","smooth"),method="lm",formula=y~poly(x,2))
library(splines)
qplot(carat,price,data=diamonds,geom=c("point","smooth"),method='lm')
qplot(carat,price,data=diamonds,geom=c("point","smooth"),method="lm")
qplot(carat,price,data=diamonds,geom=c("point","jitter"),color="red",size=2,alpha=I(1/10))
qplot(carat,price,data=diamonds,geom="jitter")
qplot(carat,price,data=diamonds,geom="point")
qplot(carat,price,data=diamonds,geom="smooth")
qplot(carat,price,data=dsmall,geom="point",fill=cut,shape=cut,alpha=I(1/10))
qplot(carat,price,data=dsmall,geom="point",fill="red",shape=cut,alpha=I(1/10))
qplot(carat,price,data=dsmall,geom="point",color="red",shape=cut,alpha=I(1/2),size=1/5)
qplot(carat,price,data=diamonds,geom="boxplot")
qplot(carat,price,data=diamonds,geom="jitter")
qplot(color,cut,data=diamonds,geom="jitter")
qplot(color,cut,data=diamonds,geom="jitter",color="red",size=1/2,alpha=I(1/10))
qplot(color,cut,data=diamonds,geom="jitter",color="red",alpha=I(1/10))
qplot(color,cut,data=diamonds,geom=c("jitter","boxplot"),color="red",alpha=I(1/10))
qplot(color,price,data=diamonds,geom="jitter",alpha=1/10)
qplot(color,price,data=diamonds,geom="jitter",alpha=1/100)
qplot(color,price,data=diamonds,geom="jitter",alpha=1/200)
qplot(color,price,data=diamonds,geom="jitter",alpha=1/100,color="red")
qplot(color,price,data=diamonds,geom="jitter",alpha=1/100,color=factor(cut))
qplot(carat,data=diamonds,geom="histogram")
qplot(carat,data=dsmall,geom="density")
qplot(carat,data=diamonds,geom="density")
qplot(carat,data=diamonds,geom="density",color=factor(cut))
??gg
qplot(carat,data=diamonds,geom="density",adjust=2,color=factor(cut))
qplot(carat,data=diamonds,geom="density",adjust=3,color=factor(cut))
qplot(carat,data=diamonds,geom="density",adjust=10,color=factor(cut))
qplot(carat,data=diamonds,geom="density",adjust=100,color=factor(cut))
qplot(carat,data=diamonds,geom="density",adjust=2,color=factor(cut),fill=I("red"))
qplot(carat,data=diamonds,geom="density",adjust=2,color=factor(cut),fill=I("green"),alpha=I(1/10))
qplot(carat,data=diamonds,geom="histogram",binwidth=1,xlim=c(0,3))
qplot(carat,data=diamonds,geom="histogram",binwidth=0.1,xlim=c(0,3))
qplot(carat,data=diamonds,geom="histogram",binwidth=0.01,xli=c(0,3))
qplot(carat,data=diamonds,geom="density",colour=color)
qplot(carat,data=diamonds,geom="histogram",fill=color)
qplot(carat,data=diamonds,geom="histogram",fill=cut,alpha=I(1/2),xlim=c(0,3),xlab="carat who",ylab="no who who",main="who")
qplot(carat,price,data=diamonds,geom="boxplot")
qplot(color,data=diamonds,geom="bar")
qplot(color,data=diamonds,geom="bar",fill=cut)
qplot(color,data=diamonds,geom="bar",fill=cut,alpha=I(1/2))
qplot(color,data=diamonds,geom="bar",weight=carat)+scale_y_continuous("carat")
qplot(color,data=diamonds,geom="bar",fill=cut,weight=carat)
qplot(date,unemploy/pop,data=economics,geom="line")
qplot(date,unemploy/pop,data=economics,geom=c("boxplot","line"))
View(economics)
qplot(date,uempmed,data=economics,geom="line",color=factor(pce))
qplot(carat,data=diamonds,facets = color~.,geom="histogram",binwidth=0.1,xlim=c(0,3))
qplot(carat,price,data=diamonds,facets=color~.,geom = "point",color=cut)
qplot(carat,price,data=diamonds,facets=cut~.,geom="point",color=color)
View(diamonds)
qplot(carat,..density..,data=diamonds,fill=cut,facets = color~.,geom="histogram",binwidth=0.1,xlim=c(0,3))
?plotmath
qplot(carat,price,data=dsmall,xlab="price",ylab="weight",main="price-weight relationship")
qplot(carat,price/carat,data=dsmall,ylab=expression(frac(price,carat)),xlab="weight",main="small diamonds",xlim=c(.2,1))
qplot(carat,price,data=dsmall,log="xy")
qplot(displ,hwy,data=mpg,colour=factor(cyl))
qplot(displ,hwy,data=mpg,colour=factor(cyl),main="yuanlairuci")
qplot(displ,hwy,data=mpg,colour=factor(cyl),mian="haodeya",xlim=c(0,9),ylim=c(1,100),alpha=I(1/2),shape=factor(cyl),size=0.5)
qplot(displ,hwy,data=mpg,facets=.~year)+geom_smooth()
qplot(displ,hwy,data=mpg,facets=year~.)+geom_smooth()
qplot(carat,price,data=diamonds,geom=c("point","smooth"),color=cut)
p<-qplot(displ,hwy,data=mpg,colour=factor(cyl))
summary(p)
save(p,file="plot.rdate")
load("plot.rdate")
ggsave("plot.png",width = 5,height = 5)
p<-qplot(carat,price,data=diamonds,color=cut,alpha=I(1/2),size=1,xlim=c(0,3),ylim=c(0,30000),xlab="carat",ylab="price",main="who are you")
save(p,file="carat_price.rdata")
ggsave("carat_price.pdf",width = 6,height = 6)
load("carat_price.rdata")
p
p<-ggplot(diamonds,aes(x=carat))
p<-p+layer(geom="bar",geom_params=list(fill="steelblue"),stat="bin",stat_params=list(binwidth=2))
geom_histogram(binwidth=2,fill="steelblue")
ggplot(msleep,aes(sleep_rem/sleep_total,awake))+geom_point()
qplot(sleep_rem/sleep_total,awake,data=msleep,geom="point")
qplot(sleep_rem/sleep_total,awake,data=msleep)+geom_smooth()
qplot(sleep_rem/sleep_total,awake,data=msleep,geom=c("point","smooth"))
ggplot(msleep,aes(sleep_rem/sleep_total,awake))+geom_point()+geom_smooth()
p<-ggplot(mtcars,aes(x=mpg,y=wt))
p+geom_point()
p+geom_point(aes(colour=factor(cyl)))
q<-p+geom_point(aes(colour=factor(cyl)))
save(q,file="qq.rdata")
ggsave("qq.pdf",width = 8,height=8)
p+geom_point(aes(u=disp))
p<-ggplot(mtcars,aes(mpg,wt))
p+geom_point(colour="darkblue")
p+geom_point(aes(colour="darkblue"))
library(nlme)
View(Oxboys)
p<-ggplot(Oxboys,aes(age,height,group=Subject))+geom_line()
p<-ggplot(Oxboys,aes(age,height,group=Subject))+geom_point()+geom_line()
p<-ggplot(Oxboys,aes(height,age,group=Occasion,facets=Subject))+geom_point()+geom_line()
p
p<-ggplot(Oxboys,aes(age,height,group=Subject))+geom_line()
p+geom_smooth(aes(group=Subject),method = "lm",se=F)
p+geom_smooth(aes(group=1),method="lm",size=2,se=F)
boysbox<-ggplot(Oxboys,aes(Occasion,height))+geom_boxplot()
boysbox
boysbox<-ggplot(Oxboys,aes(Occasion,height))+geom_boxplot(aes(fill=factor(age),color="blue"))
save(boysbox,file="boysbox.rdata")
ggsave("boysbox.pdf",width = 8,height=8)
boysbox<-ggplot(Oxboys,aes(Occasion,height))+geom_boxplot(aes(fill=factor(age),color=I("darkblue")))

smyang2018

跳至工具栏