About
About This Website
This is the website for Lexi’s final project under Prof. Bin Chen’s guidance.
Statement on AI Use
AI tools: ChatGPT, Gemini
The purpose of the assignment where AI was used:
- Data cleaning: Asking chatbots how to solve the column problem.
The original work:
I tried to separate all the industry column into 2 parts, while the csv still looks wired.
Exact prompts and AI responses
请指导我如何正确完成以下数据清理任务:改表格中industry column存在不一致,为什么会出现这一问题,如何将industry column统一到一个column中?
✅ 一、问题原因 1. CSV 行中字段数量不一致(导致无法正常 read.csv) 你文件的表头有 6 个字段: Country, Country_label, INDUSTRY, STAGE, Sum_of_deals, Year 但是从第 45 行开始出现如下情况(例子): ALL,-- All countries --,Media, social platforms and marketing,VC,3565.8076635,2014 其中 INDUSTRY 含有逗号,但没有加引号,导致 R 会把它拆成两个字段: "Media" " social platforms and marketing" 整行就变成 7 个字段,从而引发 parse 错误。 ✔ 本质:CSV 文件是损坏的,需要手动修复 INDUSTRY 中的逗号 (例如:将 Media, social platforms and marketing → "Media, social platforms and marketing" ) 2. INDUSTRY 字段中含逗号,但未加双引号(格式不符合 CSV 标准) 用于 CSV 的字段如果出现逗号,必须以引号包围,例如: "Media, social platforms and marketing" 你的文件缺少引号,导致无法解析。 ✅ 二、R Studio 清理步骤(可直接复制使用) 下面是一段稳健的 R 代码,能够: 先读入原始行(避免 read.csv 报错) 识别并修复错误字段数 把损坏的 INDUSTRY 字段合并回来 生成干净的数据框 ✔ R 脚本:清理 CSV # 1. 读取原始文本行(避免解析失败) lines <- readLines("ai_investment.csv", warn = FALSE) # 2. 拆分每行 split_lines <- strsplit(lines, ",") # 检查每行字段个数 field_counts <- sapply(split_lines, length) table(field_counts) # 正常行应当是 6 个字段 # 出现 7 个字段的行需要修复 # 3. 修复字段数为 7 的行(industry 被拆成两列) fixed_lines <- lapply(split_lines, function(x) { if (length(x) == 7) { # 把第3列和第4列合并成一个完整 INDUSTRY industry <- paste0(x[3], ",", x[4]) # 构建修复后的 6 列 return(c(x[1], x[2], industry, x[5], x[6], x[7])) } else { return(x) } }) # 4. 转成 data.frame df <- as.data.frame(do.call(rbind, fixed_lines), stringsAsFactors = FALSE) # 5. 重新命名列(因为 readLines 没有自动处理 header) colnames(df) <- df[1, ] # 第一行作为表头 df <- df[-1, ] # 去掉表头行 # 6. 转换字段类型 df$Sum_of_deals <- as.numeric(df$Sum_of_deals) df$Year <- as.integer(df$Year) df$Country <- as.factor(df$Country) df$Country_label <- as.factor(df$Country_label) df$INDUSTRY <- as.factor(df$INDUSTRY) df$STAGE <- as.factor(df$STAGE) # 7. 检查是否干净 str(df) summary(df)
