R语言数据处理(四)

R语言数据处理(四)

1. 表格合并

🚀🚀🚀代码功能: 从指定文件夹中读取所有 .xlsx 文件,并提取每个文件中特定行和列的数据,然后将这些数据合并到一个最终的数据框中,最后将合并后的数据写入新的 .xlsx 文件中。

##*##----------------------------------------------------------------------------------------
# @author:Jackson Zhao
# @time:2024年5月26日21:22:04
# @description:从指定文件夹中读取所有 .xlsx 文件,
#               并提取每个文件中特定行和列的数据,然后将这些数据合并到一个最终的数据框中,
#               最后将合并后的数据写入新的 .xlsx 文件中。

library(readxl)
library(dplyr)
library(stringr)
setwd("D:/Datasets/GD/sample result/TotalEffect")
file_list <- list.files(pattern = "*.xlsx")

# 初始化一个空的列表来存储所有文件的数据框
data_list <- list()

# 遍历所有文件,提取所需的数据
for (file in file_list) {
  data <- read_excel(file, col_names = FALSE)
  data <- as.data.frame(data)
  
  # 提取第5、8、9和10行的第5列的数据
  values <- data[c(5, 8, 10, 11), 5]
  values <- round(as.numeric(values), 2)
  # 提取第5、8、9和10行的第2列作为行名,并进行格式化
  row_names <- data[c(5, 8, 10, 11), 2]

  df <- data.frame(values)
  colnames(df) <- file
  rownames(df) <- row_names

  data_list[[file]] <- df
}
final_data <- bind_cols(data_list)

print(final_data)
write.xlsx(final_data, "Total Effect.xlsx", sheetName = "Total_Effect"
           , colNames = TRUE, rowNames= TRUE, append = TRUE)

2. 数据提取

🚀🚀🚀代码功能: 从两个数据源中提取特定样地的数据,进行合并和处理

##*******************************************************************************----
##*******************************************************************************
## @ author:JAckson Zhao
#  @ time: 2024年8月23日17:34:07
# @ description:数据提取
library(readxl)
library(dplyr)
setwd("C:\\Users\\YiPei\\Desktop\\Site index")

combined_data <- data.frame()
for(a in 1:3){
  print(a)
  b <- "XJ"
  
  dataCsv <- read.csv("样木树龄_树高.csv", sep = ",", fileEncoding = "GBK") %>%
    filter(PlotsID == a, Site == b)
  
  dataXlsx <- read_excel("样地调查.xlsx",
                         sheet = paste0(b, "-", a),
                         skip = 6) %>%
    as.data.frame() %>%
    select(1:5)%>%  # 选择前5列
    rename(编号 = `编号...1`, 植物名称 = `植物名称...2`)
  
  # 将 dataXlsx 的编号列转换为字符类型
  dataXlsx$编号 <- as.character(dataXlsx$编号)
  dataCsv$TreeID <- as.character(dataCsv$TreeID)
  
  merged_data <- dataXlsx %>%
    left_join(dataCsv, by = c("编号" = "TreeID"))%>%
    filter(!is.na(Age))
  
  # 从 merged_data 中选择特定的列并重命名
  result_data <- merged_data %>%
    select(Site, PlotsID, `编号`, `植物名称.x`, `树高.x`, Age, `胸径.x`, `备注.x`) %>%
    rename(ID = `编号`,
           Type = `植物名称.x`,
           Hight = `树高.x`,
           DBH = `胸径.x`,
           Crown = `备注.x`,
           PlotsID = PlotsID,
           Age = Age,
           Site = Site)
  
  combined_data <- rbind(combined_data, result_data)
}
combined_data
nrow(combined_data)
Final_data <- rbind(Final_data, combined_data)
nrow(Final_data)

Final_data

write.csv(Final_data, "Final_data.csv", row.names = FALSE)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jackson的生态模型

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值