|
| 1 | +R 备忘清单 |
| 2 | +=== |
| 3 | + |
| 4 | +该备忘单提供了使用 R 语言的示例,涵盖 R 语言基础知识、控制流、类型、结构/类、运算符、函数方法等 |
| 5 | + |
| 6 | +入门 |
| 7 | +--- |
| 8 | + |
| 9 | +### 获取帮助 |
| 10 | + |
| 11 | +访问帮助文件 |
| 12 | + |
| 13 | +```r |
| 14 | +?mean |
| 15 | +# 获取特定功能的帮助 |
| 16 | +help.search('weighted mean') |
| 17 | +# 在帮助文件中搜索单词或短语 |
| 18 | +help(package = 'dplyr') |
| 19 | +# 查找软件包的帮助。 |
| 20 | +``` |
| 21 | + |
| 22 | +有关对象的更多信息 |
| 23 | + |
| 24 | +```r |
| 25 | +str(iris) |
| 26 | +# 获取对象结构的摘要 |
| 27 | +class(iris) |
| 28 | +# 查找对象所属的类 |
| 29 | +``` |
| 30 | + |
| 31 | +### 使用库 |
| 32 | + |
| 33 | +```r |
| 34 | +install.packages('dplyr') |
| 35 | +# 从 CRAN 下载并安装软件包 |
| 36 | +library(dplyr) |
| 37 | +# 将包加载到会话中,使所有其功能可供使用 |
| 38 | +dplyr::select |
| 39 | +# 使用包中的特定函数 |
| 40 | +data(iris) |
| 41 | +# 将内置数据集加载到环境中。 |
| 42 | +``` |
| 43 | + |
| 44 | +### 工作目录 |
| 45 | + |
| 46 | +```r |
| 47 | +getwd() |
| 48 | +# 查找当前工作目录(其中找到输入并发送输出) |
| 49 | +setwd(‘C://file/path’) |
| 50 | +# 更改当前工作目录 |
| 51 | +``` |
| 52 | + |
| 53 | +使用 RStudio 中的项目来设置工作目录到您正在使用的文件夹 |
| 54 | + |
| 55 | +基础入门 |
| 56 | +--- |
| 57 | + |
| 58 | +### 变量和赋值 |
| 59 | + |
| 60 | +```R |
| 61 | +x <- 10 # 使用箭头赋值 |
| 62 | +y = 20 # 或者直接使用等号赋值 |
| 63 | +``` |
| 64 | + |
| 65 | +### 数据类型 |
| 66 | + |
| 67 | +```R |
| 68 | +numeric_var <- 3.14 # 数值型 |
| 69 | +character_var <- "hello" # 字符串 |
| 70 | +logical_var <- TRUE # 逻辑型 |
| 71 | +``` |
| 72 | + |
| 73 | +### 向量和列表 |
| 74 | +<!--rehype:wrap-class=row-span-2--> |
| 75 | + |
| 76 | +```R |
| 77 | +# 向量 |
| 78 | +numeric_vector <- c(1, 2, 3, 4) |
| 79 | +character_vector <- c("apple", "orange", "banana") |
| 80 | + |
| 81 | +# 列表 |
| 82 | +my_list <- list(name = "John", age = 30, city = "New York") |
| 83 | +``` |
| 84 | +<!--rehype:className=wrap-text--> |
| 85 | + |
| 86 | +向量和操作 |
| 87 | + |
| 88 | +```r |
| 89 | +# 创建向量 |
| 90 | +numbers <- c(1, 2, 3, 4, 5) |
| 91 | +# 计算向量的和 |
| 92 | +sum_result <- sum(numbers) |
| 93 | +# 计算向量的平均值 |
| 94 | +mean_result <- mean(numbers) |
| 95 | +``` |
| 96 | + |
| 97 | +### 数据框(Data Frame) |
| 98 | + |
| 99 | +```R |
| 100 | +my_df <- data.frame(name = c("John", "Alice"), age = c(30, 25)) |
| 101 | + |
| 102 | +# 创建数据框 |
| 103 | +student_data <- data.frame( |
| 104 | + name = c("John", "Alice", "Bob"), |
| 105 | + age = c(25, 23, 22), |
| 106 | + grade = c("A", "B", "C") |
| 107 | +) |
| 108 | + |
| 109 | +# 显示数据框 |
| 110 | +print(student_data) |
| 111 | +``` |
| 112 | +<!--rehype:className=wrap-text--> |
| 113 | + |
| 114 | +### 函数 |
| 115 | + |
| 116 | +```R |
| 117 | +# 定义函数 |
| 118 | +add_numbers <- function(a, b) { |
| 119 | + result <- a + b |
| 120 | + return(result) |
| 121 | +} |
| 122 | + |
| 123 | +# 调用函数 |
| 124 | +sum_result <- add_numbers(10, 5) |
| 125 | +``` |
| 126 | + |
| 127 | +### 条件语句 |
| 128 | + |
| 129 | +```R |
| 130 | +if (x > 0) { |
| 131 | + print("Positive") |
| 132 | +} else { |
| 133 | + print("Non-positive") |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +### for 循环语句 |
| 138 | + |
| 139 | +```R |
| 140 | +for (i in 1:5) { |
| 141 | + print(i) |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +### while 循环 |
| 146 | + |
| 147 | +```r |
| 148 | +counter <- 1 |
| 149 | +while (counter <= 5) { |
| 150 | + print(counter) |
| 151 | + counter <- counter + 1 |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### 数据读取和输出 |
| 156 | + |
| 157 | +```R |
| 158 | +# 读取数据 |
| 159 | +my_data <- read.csv("data.csv") |
| 160 | +# 输出数据 |
| 161 | +write.csv(my_data, "output.csv") |
| 162 | +``` |
| 163 | + |
| 164 | +### 清理工作空间 |
| 165 | + |
| 166 | +```r |
| 167 | +# 清空所有变量 |
| 168 | +rm(list = ls()) |
| 169 | +# 退出 R |
| 170 | +q() |
| 171 | +``` |
| 172 | + |
| 173 | +图形绘制 |
| 174 | +--- |
| 175 | + |
| 176 | +### 散点图 |
| 177 | + |
| 178 | +```R |
| 179 | +plot(x, y) |
| 180 | +``` |
| 181 | + |
| 182 | +### 直方图 |
| 183 | + |
| 184 | +```R |
| 185 | +hist(data) |
| 186 | +``` |
| 187 | + |
| 188 | +### 线图 |
| 189 | + |
| 190 | +```R |
| 191 | +plot(x, y, type = "l") |
| 192 | +``` |
| 193 | + |
| 194 | +### 绘制散点图 |
| 195 | + |
| 196 | +```R |
| 197 | +x <- c(1, 2, 3, 4, 5) |
| 198 | +y <- c(2, 4, 5, 6, 7) |
| 199 | +plot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis") |
| 200 | +``` |
| 201 | +<!--rehype:className=wrap-text--> |
| 202 | + |
| 203 | +### 绘制直方图 |
| 204 | + |
| 205 | +```R |
| 206 | +data <- c(1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5) |
| 207 | +hist(data, main = "Histogram", xlab = "Value", col = "lightblue") |
| 208 | +``` |
| 209 | +<!--rehype:className=wrap-text--> |
| 210 | + |
| 211 | +### 绘制折线图 |
| 212 | + |
| 213 | +```R |
| 214 | +x <- c(1, 2, 3, 4, 5) |
| 215 | +y <- c(2, 4, 5, 6, 7) |
| 216 | +plot(x, y, type = "l", main = "Line Plot", xlab = "X-axis", ylab = "Y-axis") |
| 217 | +``` |
| 218 | +<!--rehype:className=wrap-text--> |
| 219 | + |
| 220 | +向量 |
| 221 | +--- |
| 222 | + |
| 223 | +### 创建向量 |
| 224 | +<!--rehype:wrap-class=col-span-2--> |
| 225 | + |
| 226 | +:- | - | - |
| 227 | +-------|-------|------- |
| 228 | +`c(2, 4, 6)` | 2 4 6 | 将元素连接成向量 |
| 229 | +`2:6` | 2 3 4 5 6 | 整数序列 |
| 230 | +`seq(2, 3, by=0.5)` | 2.0 2.5 3.0 | 复杂的序列 |
| 231 | +`rep(1:2, times=3)` | 1 2 1 2 1 2 | 重复向量 |
| 232 | +`rep(1:2, each=3)` | 1 1 1 2 2 2 | 重复向量的元素 |
| 233 | + |
| 234 | +### 选择向量元素 |
| 235 | +<!--rehype:wrap-class=row-span-2--> |
| 236 | + |
| 237 | +#### 按位置 |
| 238 | + |
| 239 | +:- | - |
| 240 | +----|---- |
| 241 | +`x[4]` | 第四个元素 |
| 242 | +`x[-4]` | 除了第四个之外的所有 |
| 243 | +`x[2:4]` | 元素二到四 |
| 244 | +`x[-(2:4)]` | 除二到四之外的所有元素 |
| 245 | +`x[c(1, 5)]` | 元素一和元素五 |
| 246 | +<!--rehype:className=left-align--> |
| 247 | + |
| 248 | +#### 按值 |
| 249 | + |
| 250 | +:- | - |
| 251 | +----|---- |
| 252 | +`x[x == 10]` | 等于 10 的元素 |
| 253 | +`x[x < 0]` | 所有元素小于零 |
| 254 | +`x[x %in% c(1, 2, 5)]` | 集合 1, 2, 5 中的元素 |
| 255 | +<!--rehype:className=left-align--> |
| 256 | + |
| 257 | +#### 命名向量 |
| 258 | + |
| 259 | +:- | - |
| 260 | +----|---- |
| 261 | +`x['apple']` | 名为“apple”的元素。 |
| 262 | +<!--rehype:className=left-align--> |
| 263 | + |
| 264 | +### 重复向量的元素 |
| 265 | + |
| 266 | +:- | - |
| 267 | +----|---- |
| 268 | +`sort(x)` | 返回排序后的 x |
| 269 | +`rev(x)` | 返回 x 的反转 |
| 270 | +`table(x)` | 查看值的计数 |
| 271 | +`unique(x)`| 查看唯一值 |
| 272 | +<!--rehype:className=left-align--> |
| 273 | + |
| 274 | +另见 |
| 275 | +--- |
| 276 | + |
| 277 | +- [R 语言官网](https://www.r-project.org/) _(r-project.org)_ |
| 278 | +- [数据科学 R](https://r4ds.hadley.nz/) _(hadley.nz)_ |
| 279 | +- [使用 R 进行整洁的建模](https://www.tmwr.org/) _(tmwr.org)_ |
| 280 | +- [在 R 中使用 mlr3 进行应用机器学习](https://mlr3book.mlr-org.com/) _(mlr-org.com)_ |
| 281 | +- [深度学习](https://srdas.github.io/DLBook/) _(github.io)_ |
| 282 | +- [搜索任何与 R 相关的内容](https://rdrr.io/) _(rdrr.io)_ |
| 283 | +- [R 文档](https://www.rdocumentation.org/) _(rdocumentation.org)_ |
0 commit comments