render compile
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
---
|
||||
title: "Lesson 6"
|
||||
format: html
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
```{r}
|
||||
|
||||
https://rs1.drwater.net
|
||||
|
||||
username:
|
||||
- ruser01
|
||||
- ruser02
|
||||
- ruser03
|
||||
- ruser04
|
||||
- ruser05
|
||||
- ruser06
|
||||
|
||||
RWEP2025
|
||||
|
||||
```
|
||||
|
||||
|
||||
# 安装包
|
||||
|
||||
|
||||
```{r}
|
||||
install.packages("tidyverse")
|
||||
|
||||
x <- c(1:10, NA)
|
||||
|
||||
hist(x)
|
||||
|
||||
mean(x, na.rm = TRUE)
|
||||
|
||||
median(x, na.rm = TRUE)
|
||||
|
||||
sd(x, na.rm = TRUE)
|
||||
|
||||
|
||||
for(i in 1:10){
|
||||
print(i)
|
||||
}
|
||||
|
||||
|
||||
x + y + x * y
|
||||
|
||||
myfunc <- function(x, y = 3) {
|
||||
x + y + x * y
|
||||
}
|
||||
|
||||
|
||||
myfunc(1, 2)
|
||||
|
||||
|
||||
myfunc(10)
|
||||
|
||||
|
||||
c(FALSE, 2, 1:3, 3)
|
||||
|
||||
c(FALSE, 2, 1:3, 3) > 1
|
||||
|
||||
all(c(FALSE, 2, 1:3, 3) > 1)
|
||||
|
||||
|
||||
c(1L,2L,3L)
|
||||
|
||||
any(c(FALSE, 2, 1:3, 3) > 1)
|
||||
|
||||
|
||||
x <- 10
|
||||
|
||||
sin(x) = ?
|
||||
|
||||
paste("sin(x) = ", sin(x), sep = " ")
|
||||
|
||||
paste0("sin(x) = ", sin(x))
|
||||
|
||||
|
||||
substr("Monday", 1, 3)
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
# tidy
|
||||
|
||||
|
||||
```{r}
|
||||
require(readxl)
|
||||
|
||||
aqdf <-readxl::read_xlsx("../../data/airquality.xlsx", sheet = "metadf")
|
||||
|
||||
# install.packages("skimr")
|
||||
|
||||
aqdf |>
|
||||
skimr::skim()
|
||||
|
||||
# base
|
||||
|
||||
# tidyverse
|
||||
|
||||
aqdf |>
|
||||
dplyr::group_by(Area) |>
|
||||
dplyr::summarize(
|
||||
n = n(),
|
||||
lon.mean = mean(lon, na.rm = TRUE),
|
||||
lon.sd = sd(lat, na.rm = TRUE)
|
||||
) |>
|
||||
dplyr::filter(Area %in% c("北京市", "天津市", "上海市", "重庆市")) |>
|
||||
ggplot(aes(x = n, y = lon.mean)) +
|
||||
geom_point() +
|
||||
geom_line() +
|
||||
geom_errorbar(
|
||||
aes(ymin = lon.mean - lon.sd,
|
||||
ymax = lon.mean + lon.sd)
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
readxl::read_xlsx("./airquality.xlsx")
|
||||
|
||||
flights|>
|
||||
filter(dest=="IAH")|>
|
||||
group_by(year,month,day)|>summarize(n=n(),
|
||||
delay=mean(arr_delay,na.rm=TRUE))|>filter(n>10)
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../_extensions
|
||||
@@ -0,0 +1,207 @@
|
||||
---
|
||||
title: "一、R语言介绍"
|
||||
format:
|
||||
dwsd-revealjs:
|
||||
logo: _extensions/drwater/dwsd/inst/ucaslogo.png
|
||||
---
|
||||
|
||||
```{r}
|
||||
#| include: false
|
||||
#| cache: false
|
||||
lang <- "cn"
|
||||
require(tidyverse)
|
||||
require(learnr)
|
||||
knitr::opts_chunk$set(echo = TRUE)
|
||||
```
|
||||
|
||||
## R的历史
|
||||
|
||||
::: columns
|
||||
::: {.column width="85%"}
|
||||
|
||||
- 1976年:
|
||||
- 前身是S语言,由贝尔实验室John Chambers及其同事开发
|
||||
- 1993年:
|
||||
- R语言由Ross Ihaka和Robert Gentleman在奥克兰大学开发
|
||||
- R的诞生是为了提供一个强大的统计计算和图形显示的平台
|
||||
|
||||
:::
|
||||
::: {.column width="15%"}
|
||||
|
||||

|
||||
|
||||
:::
|
||||
:::
|
||||
|
||||
|
||||
- 1995年:R发布第一个公开版本
|
||||
- 2000年代: R语言逐渐成为统计学和数据科学领域重要工具
|
||||
- 2010年:Hadley Wickham发布了ggplot2包,数据可视化方面更强
|
||||
- 2016年:创建CRAN(Comprehensive R Archive Network),R包中央库
|
||||
- 至今
|
||||
- **R语言已成为数据科学和统计学领域最受欢迎的工具之一,被广泛用于数据分析、机器学习、数据可视化等领域**
|
||||
|
||||
|
||||
|
||||
## R的特点
|
||||
|
||||
### 一般特点
|
||||
|
||||
- 免费、开源、支持各个主要计算机系统。
|
||||
- 完整的程序设计语言,基于函数和对象。
|
||||
- 支持完善的数据类型,如向量、矩阵、数据框等。
|
||||
|
||||
### 技术特点
|
||||
|
||||
- 所有存在都是对象。
|
||||
- 所有动作都是函数调用。
|
||||
- 支持函数编程和对象类。
|
||||
- 是动态类型语言,运行速度相对较慢。
|
||||
|
||||
## R的参考资料
|
||||
|
||||
### 推荐参考书
|
||||
|
||||
- Hadley Wickham and Garrett Grolemund(2022). [R for Data Science](https://r4ds.hadley.nz/)
|
||||
- Hadley Wickham(2019). [Advanced R](https://adv-r.hadley.nz/)
|
||||
- Hadley Wickham(2016). [ggplot2 Elegant Graphics for Data Analysis](https://ggplot2-book.org/)
|
||||
- John M. Chambers(2008). [Software for Data Analysis-Programming with R](https://www.springer.com/gp/book/9780387759357)
|
||||
- Venables, W. N. & Ripley, B. D.(2002). [Modern Applied Statistics with S (MASS)](https://www.springer.com/gp/book/9780387954578)
|
||||
|
||||
|
||||
## R的参考资料
|
||||
|
||||
### 中文参考书
|
||||
|
||||
- 《R语言编程艺术》
|
||||
- 《R语言实战》
|
||||
- 《R语言教程与实践》
|
||||
- 《R语言数据可视化实战》
|
||||
- 《R语言从入门到精通》
|
||||
|
||||
## R的下载与安装
|
||||
|
||||
### R的下载与安装
|
||||
|
||||
- R的官方网站:[https://www.r-project.org/](https://www.r-project.org/)
|
||||
- CRAN镜像网站:[http://mirror.bjtu.edu.cn/cran/](http://mirror.bjtu.edu.cn/cran/)
|
||||
- 下载官方的R软件后按提示安装
|
||||
|
||||
|
||||
{{< video https://vimeo.com/203516510 width="600" height="400">}}
|
||||
|
||||
## RStudio
|
||||
|
||||
|
||||
### 什么是RStudio?
|
||||
|
||||
- RStudio官方网站: [https://posit.co/products/open-source/rstudio/](https://posit.co/products/open-source/rstudio/)
|
||||
- RStudio是一个集成开发环境(IDE),专门用于R语言编程和数据分析。
|
||||
- 它提供了一个直观的界面,使得编写、调试和运行R代码变得更加容易。
|
||||
|
||||
|
||||
## RStudio
|
||||
|
||||
### RStudio的功能
|
||||
|
||||
1. **代码编辑器**:提供了语法高亮、自动补全和代码折叠等功能。
|
||||
2. **控制台**:用于直接执行R代码并查看结果。
|
||||
3. **环境和历史记录**:可以查看当前加载的数据、函数和变量,以及之前执行过的命令。
|
||||
4. **图形和可视化**:RStudio内置了绘图设备,可以方便地创建各种统计图表。
|
||||
5. **文件管理器**:可直接在RStudio中管理文件和项目。
|
||||
6. **包管理器**:方便地安装、更新和管理R包。
|
||||
7. **Markdown编辑器**:支持Markdown格式,可以创建美观的文档和报告。
|
||||
|
||||
## 如何获取RStudio?
|
||||
|
||||
- RStudio可以从官方网站免费下载并安装:[RStudio官网](https://posit.co/download/rstudio-desktop/)
|
||||
- RStudio是一个强大的R编程环境,为R用户提供了丰富的功能和工具。
|
||||
- 它简化了R语言的使用,提高了数据分析和可视化的效率。
|
||||
|
||||
|
||||
{{< video https://vimeo.com/203516968 width="600" height="400">}}
|
||||
|
||||
## 扩展包:R package
|
||||
|
||||
- R有一万多个扩展软件包,提供了各种各样的功能
|
||||
- 已安装的基本R包,如base, stats, graphics等,启动R时默认载入
|
||||
- 其它扩展包需要用`library(.)`函数载入运行;或者采用`dplyr::filter(.)`方式
|
||||
|
||||
::: panel-tabset
|
||||
### Code
|
||||
|
||||
```{r}
|
||||
#| echo: true
|
||||
#| eval: false
|
||||
#| out-width: 50%
|
||||
# load a R package
|
||||
library(ggplot2)
|
||||
|
||||
# plot it based on the functions from `ggplot2` package
|
||||
mtcars |>
|
||||
dplyr::filter(cyl != 8) |>
|
||||
ggplot(aes(hp, mpg, color = am)) +
|
||||
geom_point() +
|
||||
geom_smooth(formula = y ~ x, method = "loess")
|
||||
```
|
||||
|
||||
### Output
|
||||
|
||||
```{r}
|
||||
#| echo: false
|
||||
#| out-width: 80%
|
||||
#| fig-width: 6
|
||||
#| fig-height: 3
|
||||
|
||||
library(ggplot2)
|
||||
mtcars |>
|
||||
dplyr::filter(cyl != 8) |>
|
||||
ggplot(aes(hp, mpg, color = am)) +
|
||||
geom_point() +
|
||||
geom_smooth(formula = y ~ x, method = "loess")
|
||||
```
|
||||
:::
|
||||
|
||||
|
||||
## 安装R包
|
||||
|
||||
- 以安装sos包为例。
|
||||
- 在RStudio中调用“Tools”菜单的“Install Packages”,输入或选择sos即可安装。
|
||||
- R图形界面安装
|
||||
- 如果不用RStudio, 在R图形界面选菜单“程序包-安装程序包”进行安装。
|
||||
- 在CRAN镜像选择窗口中选择中国的镜像,如“China (Beijing 2)”,然后选择要安装的扩展软件包名称即可完成下载和安装。
|
||||
- 程序安装
|
||||
|
||||
```r
|
||||
# 指定镜像网站并安装扩展包
|
||||
options(repos = c(CRAN = "https://mirror.tuna.tsinghua.edu.cn/CRAN/"))
|
||||
install.packages("sos")
|
||||
|
||||
# 当R包不是CRAN标准包,代码放在[github](https://github.com)
|
||||
if (!require(devtools)) install.packages('devtools')
|
||||
devtools::install_github("kjhealy/socviz")
|
||||
```
|
||||
|
||||
## 安装R包
|
||||
|
||||
{{< video https://vimeo.com/203516241 width="600" height="400">}}
|
||||
|
||||
|
||||
## 结语
|
||||
|
||||
- R语言是一种强大的统计计算和数据分析工具,适用于各种领域。
|
||||
- 通过学习R语言,可以进行数据处理、统计分析和数据可视化。
|
||||
|
||||
|
||||
## 欢迎讨论!{.center}
|
||||
|
||||
```{r}
|
||||
#| results: 'asis'
|
||||
rmdify::slideend(
|
||||
wechat = FALSE,
|
||||
type = "public",
|
||||
tel = FALSE,
|
||||
thislink = "../"
|
||||
)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user