R语言学习日记(3)-《153分钟学会R语言》

#构建子集
#基本方法使用:[] \ [[]] \ $
> x <- 1:10
> x[1]
[1] 1
> x[!:5]
Error: unexpected ':' in "x[!:"
> x[1:5]
[1] 1 2 3 4 5
> x>5
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
 [9]  TRUE  TRUE
> x[x>5]
[1]  6  7  8  9 10
> x[x>5 & x<7]
[1] 6
> x[x<3 | x>7]
[1]  1  2  8  9 10
> y <- 1:4
> names(y) <- c("a" , "b" , "c" , "d")
> y
a b c d 
1 2 3 4 
> y[2]
b 
2 
> y["b"]
b 
2 
#关于作图
> grid(NA , 5 , lwd = 2) #为纵坐标添加网格线
> grid(5 , 5 , lwd = 2) #为横坐标添加网格线

> plot(0 , main = paste(strwrap("This is a really long title that i can not type it properly" , width = 50),collapse = "\n"))  #为图形添加比较长的标题

> plot(0 , 0 , xaxt = "n" , type = "n" ,ylim = c(0 ,100) , las =1) 

> #坐标y上的数字如何水平放置
> mtext("35" , side = 2 , at = 35 , line = 1 , las = 1)  #在Y轴上添加了35这个坐标
  • 设置渐变色彩:
> x <- 1:10; names(x) <- letters[1:10] #给数组取名字
> barplot(x , col = rev(heat.colors(10))) #reverse,颠倒原来的顺序
> barplot(x , col = heat.colors(10)) #对应下图
> #R颜色组函数有rainbow()/heat.colors()/terrain.colors()/topo.colors()/cm.color()/grey()/gay()

在这里插入图片描述

  • 缺失值
    NaN,是数值型缺失值;
    NA有类型属性:integer NA , character NA等。
    判断是哪种形式的缺失值:is.na() 或 is.nan(),举例如下:
> x <- c(NA , 1 , 2)
> is.na(x)
[1]  TRUE FALSE FALSE
> is.nan(x)
[1] FALSE FALSE FALSE
> x[1] <- NaN
> is.nan(x)
[1]  TRUE FALSE FALSE
> is.na(x)
[1]  TRUE FALSE FALSE
#na不属于NAN,但nan属于NA
  • Date型
#比较两种返回当前时间的不同方式
> x <- date() #第一种
> x
[1] "Wed Jan 15 21:12:14 2020"
> class(x)
[1] "character"  #是字符型
> x2<- Sys.Date()  #第二种返回当前系统时间的方法
> x2
[1] "2020-01-15"
> class(x2)
[1] "Date" #是Date型
> weekdays(x2)
[1] "星期三"
> months(x2)
[1] "一月"
> quarters(x2) #季度
[1] "Q1"
> julian(x2) #距离规定起始时间有多少天
[1] 18276
attr(,"origin")
[1] "1970-01-01"
> x4 <- as.Date("2016-1-1")
> class(x4)
[1] "Date"
> as.numeric(x2-x4)  #Date型可以做减法,再将其转化成数值型
[1] 1475
> windows(width = 20 , height =20 ) # adjust the size of graph
> pdf("picture.pdf" , height = 4 ,width = 6 ) #设置为PDF型输出图片
> dev.off()
windows 
      2 

> n <- 100
> x <- cumsum(rnorm(n))
> y <- cumsum(rnorm(n))
> plot(x , y , type = 'l')  #type参数值是小写的L
> windows(width = 20 , height = 20);
> plot(1:3)
> dev.off()
pdf   #可以注意到这里是关闭PDF型图片
  4 
> windows(width = 20 , height = 20);
> plot(x ,y , type = 'l')
> #获得连接若干点的平滑曲线
> #情况1,已知点的函数,使用curve(expr , from , to , add = T)
> #condition2: 立方曲线差值函数,spline(x , y , n = ),示例如下:
> x <- 1:5
> y <- c(1,3,4,2.5,2)
> plot(x,y)
> sp <- spline(x , y , n = 50)
> lines(sp)
> #把数值矩阵,映射为一个颜色方格矩阵
> #使用image()或filled.contour()
> x <- y <- seq(-10 , 10 , length = 50)
> f <- function(x , y){
+ r <- sqrt(x^2 + y^2)
+ 10*sin(r)/r}
> z <- outer(x , y , f)
> image(x , y , z)
> filled.contour(x , y , z)

> x <- 1:10
> y <- runif(10)
> symbols(x , y ,circles = y/2,inches =F ,bg = x) #散点图中的散点大小同因变量的比例画出
> #这是因为绘图参数可以使用变量
> #在直方图的空白位置添加另外小图,使用函数par()
> x <- rnorm(100)
> hist(x)
> op <- par(fig = c(.02 , .5 , .5 , .98) , new = TRUE)
> boxplot(x) #添加箱线图
> #绘制椭圆或双曲线
> t <- seq(0 , 2*pi , length = 100)
> x <- sin(t)
> y <- 2*cos(t)
> plot(x , y , type = "l")
> t <- seq(0 , 2*pi , 10)
> t
[1] 0
> 
> t <- seq(0 , 2*pi , length = 10)
> t
 [1] 0.0000000 0.6981317 1.3962634 2.0943951 2.7925268 3.4906585 4.1887902
 [8] 4.8869219 5.5850536 6.2831853
> #交叉列联表使用table(x , y)
> #table(x)为x的频数表
>  x <- with(airquality , table(cut(Temp , quantile(Temp)) , Month))
> prop.table(x , 1)
        Month
                  5          6          7          8          9
 (56,72] 0.63157895 0.07894737 0.00000000 0.02631579 0.26315789
 (72,79] 0.12195122 0.36585366 0.04878049 0.21951220 0.24390244
 (79,85] 0.02564103 0.17948718 0.48717949 0.17948718 0.12820513
 (85,97] 0.00000000 0.14705882 0.29411765 0.41176471 0.14705882
 #线性回归部分
> #fitted.values --模型拟合值
> #residuals --模型残差
> #coefficients --回归系数
> #海量数据使用的回归模拟,biglm包中的biglm()
发布了109 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43448491/article/details/103991885