例如:7050_IMAG0014_S10_N01 位置为CSV文件的第五列
提取编码SXX和NXX
library(readr)
library(stringr)
library(dplyr)
# CSV文件路径
input_file <- "C:/Users/HUAWEI/Desktop/我的论文/红外相机文件信息汇总.csv"
output_file <- "E:/开题+中期+毕业文件/红外相机数据/第一批.csv"
# 读取含中文的CSV
data <- read_csv(
input_file,
locale = locale(encoding = "GB18030"),
show_col_types = FALSE
)
# 获取第5列名称
column_5 <- names(data)[5]
# S01-S500,或者STR、END
species_pattern <- paste0(
"(?i)(?<![A-Za-z0-9])",
"(?:S(?:0[1-9]|[1-9][0-9]?|[1-4][0-9]{2}|500)|STR|END)",
"(?![A-Za-z0-9])"
)
# N01-N100
number_pattern <- paste0(
"(?i)(?<![A-Za-z0-9])",
"N(?:0[1-9]|[1-9][0-9]?|100)",
"(?![A-Za-z0-9])"
)
# 提取信息
result <- data %>%
mutate(
物种 = str_to_upper(
str_extract(
as.character(.data[[column_5]]),
species_pattern
)
),
数量 = str_to_upper(
str_extract(
as.character(.data[[column_5]]),
number_pattern
)
)
)
write_excel_csv(
result,
output_file,
na = ""
)
cat("处理完成:", output_file, "\n")