library(pacman)
p_load(tidyverse,stringr,DT,skimr,DataExplorer,grf,glmnet,caret,tidytext,
explore,patchwork,ggrepel,ggcorrplot,gghighlight,ggthemes,fpp2,
forecast,magrittr,readxl,writexl,listviewer,car,tseries,vtable)
R语言是免费、开源、自由的计算平台使用成本低
R的基础语法简单、学习速度快、上手容易
R语言的数据可视化能力强,提供了丰富的绘图函数
扩展软件包发展速度快、更新快,目前已有数千个扩展包覆盖几乎所有的科学计算领域
虽然运行速度比C/C++等慢,但是可以快速测试各种算法,节约项目实验、研究时间。
很多时候只需一行代码就可以实行一项复杂的功能
R语言的学习、研究社区发展快、学习资料丰富
## [1] 1.5
## [1] 1.5 2.3 8.6 7.4 9.2
## [1] "MSFT" "GOOG" "AAPL"
## [1] TRUE FALSE TRUE TRUE FALSE FALSE
## [1] 3.141593
## [1] 1.414
## [1] 1860
## x = 1.5
## [1] "cnfont" "enfont" "GSPC" "szSymbolName" "t"
## [6] "x" "y1" "y2" "y3" "z"
## [1] "a==1"
## [1] "a > 1"
## [1] "a != 1"
# 多重分支结构,同样每个else必须和前面的}紧紧粘在一起
a <- 4
if(a == 1) {
print("a == 1")
} else if (a == 2) {
print("a == 2")
} else{
print("Not 1 %2")
}
## [1] "Not 1 %2"
## [1] 3.1416
# switch语句的多重分支结构
n <- 1
switch(n,
print("选项1"),
print("选项2"),
print("选项3"),
print("选项4"),
print("选项5")
)
## [1] "选项1"
# for 循环结构
iTotal <- 0
for(i in 1:100) # 使用关键词in枚举向量中的每一整数
{
iTotal <- iTotal + i
}
cat("1-100的累加和为:",iTotal,"\n",sep="")
## 1-100的累加和为:5050
# 字符串也同样可以成功枚举十分方便
szSymbols <- c("MSFT","GOOG","AAPL","INTL","ORCL","SYMC")
for(SymbolName in szSymbols)
{
cat(SymbolName,"\n",sep="")
}
## MSFT
## GOOG
## AAPL
## INTL
## ORCL
## SYMC
# while循环
i <- 1
iTotal <- 0
while(i <= 100)
{
iTotal <- iTotal + i
i <- i + 1
}
cat("1-100的累加和为:",iTotal,"\n",sep="")
## 1-100的累加和为:5050
# repeat循环
i <- 1
iTotal <- 0
repeat # 无条件循环,必须在程序内部设法退出
{
iTotal <- iTotal + i
i <- i + 1
if(i <= 100) next else break # 注意:next,break的用法
}
cat("1-100的累加和为:",iTotal,"\n",sep="")
## 1-100的累加和为:5050
# 自定义函数
# 注意:建立功能丰富、庞大、专业的自定义函数库、类库是公司的核心竞争力
# pt <- function() { szCurTime <- as.character.Date(Sys.time()); options(prompt=paste(szCurTime,">",sep="")) }
#pt()
# 定义自己的二元运算符,%anything%,两个百分号之间可以是任何字符串
# 定义二元运算符的过程和编写自定义函数本质相同
"%g%" <- function(x,y)
{
print(x+y)
print(x-y)
print(x*y)
print(x/y)
}
3%g%5
## [1] 8
## [1] -2
## [1] 15
## [1] 0.6
## starting httpd help server ... done
## [1] ".doTracePrint" "dev.print"
## [3] "knit_print.trunc_mat" "ls.print"
## [5] "naprint" "princomp"
## [7] "print" "print"
## [9] "print.AsIs" "print.by"
## [11] "print.condition" "print.connection"
## [13] "print.cv.glmnet" "print.data.frame"
## [15] "print.Date" "print.default"
## [17] "print.difftime" "print.Dlist"
## [19] "print.DLLInfo" "print.DLLInfoList"
## [21] "print.DLLRegisteredRoutines" "print.eigen"
## [23] "print.factor" "print.function"
## [25] "print.hexmode" "print.libraryIQR"
## [27] "print.listof" "print.NativeRoutineList"
## [29] "print.noquote" "print.numeric_version"
## [31] "print.octmode" "print.packageInfo"
## [33] "print.POSIXct" "print.POSIXlt"
## [35] "print.proc_time" "print.restart"
## [37] "print.rle" "print.simple.list"
## [39] "print.srcfile" "print.srcref"
## [41] "print.summary.table" "print.summary.warnings"
## [43] "print.summaryDefault" "print.table"
## [45] "print.train" "print.warnings"
## [47] "printCoefmat" "printHypothesis"
## [49] "printSpMatrix" "printSpMatrix2"
## [51] "sprintf" "win.print"
## [53] "withAutoprint"
##
##
## demo(graphics)
## ---- ~~~~~~~~
##
## > # Copyright (C) 1997-2009 The R Core Team
## >
## > require(datasets)
##
## > require(grDevices); require(graphics)
##
## > ## Here is some code which illustrates some of the differences between
## > ## R and S graphics capabilities. Note that colors are generally specified
## > ## by a character string name (taken from the X11 rgb.txt file) and that line
## > ## textures are given similarly. The parameter "bg" sets the background
## > ## parameter for the plot and there is also an "fg" parameter which sets
## > ## the foreground color.
## >
## >
## > x <- stats::rnorm(50)
##
## > opar <- par(bg = "white")
##
## > plot(x, ann = FALSE, type = "n")
##
## > abline(h = 0, col = gray(.90))
##
## > lines(x, col = "green4", lty = "dotted")
##
## > points(x, bg = "limegreen", pch = 21)
##
## > title(main = "Simple Use of Color In a Plot",
## xlab = "Just a Whisper of a Label",
## col.main = "blue", col.lab = gray(.8),
## cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)
##
## > ## A little color wheel. This code just plots equally spaced hues in
## > ## a pie chart. If you have a cheap SVGA monitor (like me) you will
## > ## probably find that numerically equispaced does not mean visually
## > ## equispaced. On my display at home, these colors tend to cluster at
## > ## the RGB primaries. On the other hand on the SGI Indy at work the
## > ## effect is near perfect.
## >
## > par(bg = "gray")
##
## > pie(rep(1,24), col = rainbow(24), radius = 0.9)
##
## > title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3)
##
## > title(xlab = "(Use this as a test of monitor linearity)",
## cex.lab = 0.8, font.lab = 3)
##
## > ## We have already confessed to having these. This is just showing off X11
## > ## color names (and the example (from the postscript manual) is pretty "cute".
## >
## > pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
##
## > names(pie.sales) <- c("Blueberry", "Cherry",
## "Apple", "Boston Cream", "Other", "Vanilla Cream")
##
## > pie(pie.sales,
## col = c("purple","violetred1","green3","cornsilk","cyan","white"))
##
## > title(main = "January Pie Sales", cex.main = 1.8, font.main = 1)
##
## > title(xlab = "(Don't try this at home kids)", cex.lab = 0.8, font.lab = 3)
##
## > ## Boxplots: I couldn't resist the capability for filling the "box".
## > ## The use of color seems like a useful addition, it focuses attention
## > ## on the central bulk of the data.
## >
## > par(bg="cornsilk")
##
## > n <- 10
##
## > g <- gl(n, 100, n*100)
##
## > x <- rnorm(n*100) + sqrt(as.numeric(g))
##
## > boxplot(split(x,g), col="lavender", notch=TRUE)
##
## > title(main="Notched Boxplots", xlab="Group", font.main=4, font.lab=1)
##
## > ## An example showing how to fill between curves.
## >
## > par(bg="white")
##
## > n <- 100
##
## > x <- c(0,cumsum(rnorm(n)))
##
## > y <- c(0,cumsum(rnorm(n)))
##
## > xx <- c(0:n, n:0)
##
## > yy <- c(x, rev(y))
##
## > plot(xx, yy, type="n", xlab="Time", ylab="Distance")
##
## > polygon(xx, yy, col="gray")
##
## > title("Distance Between Brownian Motions")
##
## > ## Colored plot margins, axis labels and titles. You do need to be
## > ## careful with these kinds of effects. It's easy to go completely
## > ## over the top and you can end up with your lunch all over the keyboard.
## > ## On the other hand, my market research clients love it.
## >
## > x <- c(0.00, 0.40, 0.86, 0.85, 0.69, 0.48, 0.54, 1.09, 1.11, 1.73, 2.05, 2.02)
##
## > par(bg="lightgray")
##
## > plot(x, type="n", axes=FALSE, ann=FALSE)
##
## > usr <- par("usr")
##
## > rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black")
##
## > lines(x, col="blue")
##
## > points(x, pch=21, bg="lightcyan", cex=1.25)
##
## > axis(2, col.axis="blue", las=1)
##
## > axis(1, at=1:12, lab=month.abb, col.axis="blue")
##
## > box()
##
## > title(main= "The Level of Interest in R", font.main=4, col.main="red")
##
## > title(xlab= "1996", col.lab="red")
##
## > ## A filled histogram, showing how to change the font used for the
## > ## main title without changing the other annotation.
## >
## > par(bg="cornsilk")
##
## > x <- rnorm(1000)
##
## > hist(x, xlim=range(-4, 4, x), col="lavender", main="")
##
## > title(main="1000 Normal Random Variates", font.main=3)
##
## > ## A scatterplot matrix
## > ## The good old Iris data (yet again)
## >
## > pairs(iris[1:4], main="Edgar Anderson's Iris Data", font.main=4, pch=19)
##
## > pairs(iris[1:4], main="Edgar Anderson's Iris Data", pch=21,
## bg = c("red", "green3", "blue")[unclass(iris$Species)])
##
## > ## Contour plotting
## > ## This produces a topographic map of one of Auckland's many volcanic "peaks".
## >
## > x <- 10*1:nrow(volcano)
##
## > y <- 10*1:ncol(volcano)
##
## > lev <- pretty(range(volcano), 10)
##
## > par(bg = "lightcyan")
##
## > pin <- par("pin")
##
## > xdelta <- diff(range(x))
##
## > ydelta <- diff(range(y))
##
## > xscale <- pin[1]/xdelta
##
## > yscale <- pin[2]/ydelta
##
## > scale <- min(xscale, yscale)
##
## > xadd <- 0.5*(pin[1]/scale - xdelta)
##
## > yadd <- 0.5*(pin[2]/scale - ydelta)
##
## > plot(numeric(0), numeric(0),
## xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd,
## type = "n", ann = FALSE)
##
## > usr <- par("usr")
##
## > rect(usr[1], usr[3], usr[2], usr[4], col="green3")
##
## > contour(x, y, volcano, levels = lev, col="yellow", lty="solid", add=TRUE)
##
## > box()
##
## > title("A Topographic Map of Maunga Whau", font= 4)
##
## > title(xlab = "Meters North", ylab = "Meters West", font= 3)
##
## > mtext("10 Meter Contour Spacing", side=3, line=0.35, outer=FALSE,
## at = mean(par("usr")[1:2]), cex=0.7, font=3)
##
## > ## Conditioning plots
## >
## > par(bg="cornsilk")
##
## > coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "green3")
##
## > par(opar)