R语言数据科学基础:从环境配置到核心统计分析(v2024.08.28)
ggplot2进行数据可视化。目的在于确保团队成员能够快速建立可操作的数据分析流程,提高工作效率和分析结果的可复现性。
1. 环境准备与数据导入
此节详细阐述R语言环境的部署以及标准数据格式的导入流程。确保分析起点的一致性和有效性。
1.1 R及RStudio安装(版本:R 4.3.0, RStudio 2023.09.1)
请根据操作系统选择对应的安装包。建议使用官方源进行下载。
R语言核心程序下载:
访问 CRAN 官方镜像下载对应版本。例如,Windows 用户可通过 CRAN Windows Base 获取。
R下载
执行下载并安装。使用默认安装路径。
RStudio IDE下载:
访问 RStudio 官方下载页面,选择免费桌面版 (posit.co/download/rstudio-desktop)。
RStudio下载
执行下载并安装。建议安装在R语言默认路径下。
1.2 常用库安装与数据导入
为确保后续统计分析及可视化功能完整,需安装必要依赖库。
安装核心依赖库:
在RStudio控制台中执行以下命令:
install.packages(c("tidyverse", "readxl", "data.table")) # Expected Output: # installing the source package ‘tidyverse’ # installing the source package ‘readxl’ # installing the source package ‘data.table’ # ... (installation logs) ...tidyverse集合了数据操作与可视化工具;readxl用于Excel文件读取;data.table提供高效的数据处理能力。
Note: 安装过程中若提示选择CRAN镜像,请选择靠近物理位置的服务器以加速下载。
CSV文件导入示例:
使用
readr包(tidyverse的一部分)进行CSV文件导入。假定当前工作目录下存在data.csv文件。library(readr) data <- read_csv("data.csv") # Expected Output: # Rows: 1000 Columns: 5 # -- Column specification -------------------------------------------------------- # Delimiter: "," # chr (1): Category # dbl (4): ID, Value1, Value2, Date # # i Use `spec()` to retrieve the full column specification for this data. # i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Warning: 确保
data.csv文件路径正确。可通过getwd()查看当前工作目录,setwd("your/path")修改工作目录。getwd() # Expected Output: # [1] "/Users/youruser/Documents"
2. 描述性统计与假设检验基础
本节覆盖核心的描述性统计量计算及常用假设检验方法。
2.1 描述性统计概览
对导入的数据集进行初步统计描述,了解数据分布特征。
数据集概览:
summary(data) # Expected Output: # ID Value1 Value2 Category Date # Min. : 1.0 Min. : -5.00 Min. : 0.000 Length:1000 Min. :1.00 # 1st Qu.:250.8 1st Qu.: 5.00 1st Qu.: 8.000 Class :character 1st Qu.:2.00 # Median :500.5 Median : 10.00 Median :15.000 Mode :character Median :3.00 # Mean :500.5 Mean : 10.05 Mean :14.950 Mean :3.05 # 3rd Qu.:750.2 3rd Qu.: 15.00 3rd Qu.:22.000 3rd Qu.:4.00 # Max. :1000.0 Max. : 25.00 Max. :30.000 Max. :5.00特定变量统计量:
sd(data$Value1) # 标准差 # Expected Output: # [1] 5.795321 quantile(data$Value2, probs = c(0.25, 0.75)) # 四分位数 # Expected Output: # 25% 75% # 8.0 22.0
2.2 T检验应用实例
使用T检验评估两组均值是否存在显著差异。
数据准备:
假设
data中Category字段包含"A"和"B"两组,我们想比较Value1在这两组间的均值差异。group_a <- subset(data, Category == "A")$Value1 group_b <- subset(data, Category == "B")$Value1执行T检验:
t.test(group_a, group_b) # Expected Output: # # Welch Two Sample t-test # # data: group_a and group_b # t = -1.2345, df = 998.76, p-value = 0.2173 # alternative hypothesis: true difference in means is not equal to 0 # 95 percent confidence interval: # -1.2345 0.4567 # sample estimates: # mean of x mean of y # 10.1234 10.3456根据p-value判断显著性。
p < 0.05通常被认为是统计显著。
3. 数据可视化实践 (ggplot2)
使用ggplot2创建高质量统计图形,以直观展示数据模式。
3.1 分布图绘制
直方图和密度图用于展示单变量的分布形态。
创建直方图:
library(ggplot2) ggplot(data, aes(x = Value1)) + geom_histogram(binwidth = 1, fill = "steelblue", color = "black") + labs(title = "Value1 分布直方图", x = "Value1", y = "频数") # Expected Output: (Plot area will display a histogram)创建密度图:
ggplot(data, aes(x = Value1)) + geom_density(fill = "lightblue", alpha = 0.5) + labs(title = "Value1 密度估计图", x = "Value1", y = "密度") # Expected Output: (Plot area will display a density plot)
3.2 散点图与箱线图
展示变量间的关系及分组对比。
创建散点图:
探索
Value1与Value2之间的关系。ggplot(data, aes(x = Value1, y = Value2, color = Category)) + geom_point(alpha = 0.6) + labs(title = "Value1 vs Value2 散点图", x = "Value1", y = "Value2") + theme_minimal() # Expected Output: (Plot area will display a scatter plot)创建箱线图:
比较不同
Category下Value1的分布。ggplot(data, aes(x = Category, y = Value1, fill = Category)) + geom_boxplot() + labs(title = "按类别分组的 Value1 分布箱线图", x = "类别", y = "Value1") + theme_classic() # Expected Output: (Plot area will display a box plot)
Note: 图层叠加 (+) 是ggplot2的强大特性。使用labs()自定义图表标题和轴标签。theme_minimal()或theme_classic()等用于快速调整图表主题。
4. 延伸阅读与资源
进阶学习资源。
R语言统计分析教程
R基础教程
ggplot2绘图教程
References
- R Project for Statistical Computing: https://www.r-project.org/
- Posit (RStudio) Official Site: https://posit.co/
- Roxi.cc : wizzegroup.com