admin管理员组

文章数量:1530037

        工作需要了解数据分析方面的相关知识,起初对于这方面第一反应就是Python,虽说Python对数据分析,画图等都有很好的方面,但在搜索Python相关内容时百度词条第一条就是Python与R的相关内容,起初觉得刚入行再去了解一门新的语言会很困难,但在接触过后发现R语言就是后端中的HTML,加上R拥有丰富的开源库资源编程异常简单,同时在B站了解过后,推荐配合使用AI进行辅助编程有奇效,写此篇用于笔记作用。

环境准备

        R语言的环境配置非常简单,在R的官方网站(CRAN: Mirrors)进入镜像网址

选择China第一个,随后

选择Download R for Windows

        如果你是第一次安装R,选择

        install R for the first time

        选择Download R-4.4.1 for Windows(82 megabytes,64 bit)按照提示下载完安装包无脑下一步完成安装即可。

RStudio

        安装完R环境后你将获得一个,忽略它,自带的编辑器比较原始,R的编辑器通常采用RStudio进行相关的编辑。

        进入官网(CRAN: Mirrors),下拉选择

选择2:Install RStudio等待下载完成即可,安装也是无脑下一步安装等待安装完成。

完成之后打开界面,得到的界面如图所示

        其中脚本编辑区域需要点击file ->new file ->RScript建立脚本编辑区。在控制台区的Console中虽可以进行相关操作,单操作过程繁琐,因此建议使用RScript进行编写。

        同时,在执行的过程中如果提示缺少包资源等,按照提示在Console中输入命令进行安装即可。

案例

        本案例选择的数据集并不专业,字段繁多,仅用于展示使用。

        由于R语言的语法相对简单且拥有大量的开源库,记住几行代码其余功能交由ChatGPT完成即可。

        其中最常用的为设置工作空间和读取文件的操作。

#设置工作空间
setwd("C:\\Users\\jl\\Desktop\\DataAnalysis")
#读取文件
df1 <- read.csv("xxx.csv",row.names = 1)

        注意:在复制路径后记得将路径中的单斜线“\”改为 双斜线“\\”。

        RStudio的运行方式不同于其它,需要用光标选中你要执行的代码片段,或者把光标放在需要执行的第一行代码后,点击脚本编辑区域右上侧的“Run”进行执行操作。

        案例一:热图

        在完成设置工作空间和文件读取的操作后将需求发送给ChatGPT,同时需要注意提问方式,使用AI最重要的就是需要会问。

        例如:I created a data frame named df1 in R language, which has 527 columns and 1000 rows. All variables are numbers and all fields are strings. Now we need to extract IP3_15, IP2/178, IP3_225, IP3_226, and IP3_224 to draw a correlation heatmap. Please show the code.        

        (我用R语言创建了一个名为df1的数据帧,它有527列1000行。所有变量都是数字,所有字段都为字符串,现在需要截取 IP3_15,IP2_178,IP3_225,IP3_226,IP3_224绘制相关性热图,请展示代码。)

        代码如下:

#--------------------------方格式热图------------------------------
setwd("C:\\Users\\jl\\Desktop\\DataAnalysis")
df1 <- read.csv("回转窑数据.csv",row.names = 1)
# 提取所需列
selected_columns <- df1[, c("IP3_15", "IP2_178", "IP3_225", "IP3_226", "IP3_224")]

# 计算相关矩阵
cor_matrix <- cor(selected_columns, use = "complete.obs")

# 加载 pheatmap 包
library(pheatmap)

# 绘制相关热图
pheatmap(cor_matrix, display_numbers = TRUE, main = "Selected Columns Correlation Heatmap")
# 计算相关矩阵
cor_matrix <- cor(selected_columns, use = "complete.obs")

# 将相关矩阵转换为字符串
cor_matrix_string <- capture.output(print(cor_matrix))

# 打印相关矩阵字符串
cat(cor_matrix_string, sep = "\n")

        执行效果如图所示:

        也可将方格式热图改为圆点式热图,值得注意的是RStudio直接将正方形的图形改为了上三角式来方便查看字段之间的相关性。

# 设置工作目录
setwd("C:\\Users\\jl\\Desktop\\DataAnalysis")

# 读取 CSV 文件
df1 <- read.csv("回转窑数据.csv", row.names = 1)

# 提取所需列
selected_columns <- df1[, c("IP3_15", "IP2_178", "IP3_225", "IP3_226", "IP3_224")]

# 计算相关矩阵
cor_matrix <- cor(selected_columns, use = "complete.obs")

# 安装并加载 corrplot 包
if (!require("corrplot")) {
  install.packages("corrplot")
}
library(corrplot)

# 绘制圆点式相关矩阵热图
corrplot(cor_matrix, method = "circle", type = "upper", 
         tl.col = "black", tl.srt = 45, addCoef.col = "black", 
         title = "Selected Columns Correlation Circle Heatmap")

执行效果如下图所示

        案例二:折线图

        同样将需求发送给ChatGPT,编辑代码:

# 设置工作目录
setwd("C:\\Users\\jl\\Desktop\\DataAnalysis")

# 读取 CSV 文件
df1 <- read.csv("回转窑数据.csv", row.names = 1)

# 提取所需列
selected_columns <- df1[, c("IP3_15", "IP2_178", "IP3_225", "IP3_226", "IP3_224")]

# 添加 ID 列作为行名称
selected_columns$id <- rownames(selected_columns)

# 加载 ggplot2 和 tidyr 包
if (!require("ggplot2")) {
  install.packages("ggplot2")
}
if (!require("tidyr")) {
  install.packages("tidyr")
}
library(ggplot2)
library(tidyr)

# 将数据转换为长格式以便 ggplot2 使用
df_long <- pivot_longer(selected_columns, cols = -id, names_to = "variable", values_to = "value")

# 绘制折线图
ggplot(df_long, aes(x = as.numeric(id), y = value, color = variable, group = variable)) +
  geom_line() +
  labs(x = "ID", y = "值", title = "所选字段的折线图") +
  theme_minimal()

展示效果:

同时RStudio也支持将折线图表制作为动态可交互式的,其代码部分为:

# 设置工作目录
setwd("C:\\Users\\jl\\Desktop\\DataAnalysis")

# 读取 CSV 文件
df1 <- read.csv("回转窑数据.csv", row.names = 1)

# 提取所需列
selected_columns <- df1[, c("IP3_15", "IP2_178", "IP3_225", "IP3_226", "IP3_224")]

# 添加 ID 列作为行名称
selected_columns$id <- rownames(selected_columns)

# 加载 ggplot2 和 tidyr 包
if (!require("ggplot2")) {
  install.packages("ggplot2")
}
if (!require("tidyr")) {
  install.packages("tidyr")
}
if (!require("plotly")) {
  install.packages("plotly")
}
library(ggplot2)
library(tidyr)
library(plotly)

# 将数据转换为长格式以便 ggplot2 使用
df_long <- pivot_longer(selected_columns, cols = -id, names_to = "variable", values_to = "value")

# 绘制折线图
p <- ggplot(df_long, aes(x = as.numeric(id), y = value, color = variable, group = variable)) +
  geom_line() +
  labs(x = "ID", y = "值", title = "所选字段的动态折线图") +
  theme_minimal()

# 将 ggplot2 图表转换为 plotly 图表
ggplotly(p)

        展示效果如下:

同时值得注意的是,如果你对图片大小不满意可以点击图片上面的zoom单独将图片拉拽出来

        其余交互还未进行探索。

案例三:打印相关性系数

        同样的,只不过该结果显示在Console中。

# 设置工作目录
setwd("C:\\Users\\jl\\Desktop\\DataAnalysis")

# 读取 CSV 文件
df1 <- read.csv("回转窑数据.csv", row.names = 1)

# 仅保留数值列
numeric_columns <- sapply(df1, is.numeric)
df_numeric <- df1[, numeric_columns]

# 计算每个数值字段与 IP3_15 的相关性系数
correlation_with_IP3_15 <- sapply(df_numeric, function(x) cor(x, df_numeric$IP3_15, use = "complete.obs"))

# 将结果转换为数据框
correlation_df <- data.frame(Field = names(correlation_with_IP3_15), Correlation = correlation_with_IP3_15)

# 按相关性系数绝对值排序
correlation_df <- correlation_df[order(abs(correlation_df$Correlation), decreasing = TRUE), ]

# 打印相关性系数
print(correlation_df)

        结果:

结束

        此篇仅作笔记之用,漏洞还很多。

本文标签: 分析预测语言数据chatGPT