xiaotu9316 2024-06-03 15:35 采纳率: 88.6%
浏览 4
已结题

R语言:合并提取生成的excel,总是少了第一个循环值对应的结果

R语言:合并提取生成的excel,总是少了第一个循环值对应的结果,不知道错在哪,如何修改代码

(是merge函数用错了吗?)

1、for循环输出的结果提示是正确的,共11个:


```r
> library(readxl)
> library(openxlsx)
> 
> exposure_vals <- c("GCST90274765","GCST90274767","GCST90274773","GCST90274780","GCST90274787","GCST90274766","GCST90274784","GCST90274795","GCST90274824","GCST90274840","GCST90274841")
> 
> for (i in 1:length(exposure_vals)) {
+   
+   exposure_val <- exposure_vals[i]
+   print(exposure_val)
+ }
[1] "GCST90274765"
[1] "GCST90274767"
[1] "GCST90274773"
[1] "GCST90274780"
[1] "GCST90274787"
[1] "GCST90274766"
[1] "GCST90274784"
[1] "GCST90274795"
[1] "GCST90274824"
[1] "GCST90274840"
[1] "GCST90274841"
>

2、但是继续运行下面的代码,生成的excel结果总是少了第一个循环值“"GCST90274765"对应的结果(两个源表中确定都有对应的行,excel能检索到,excel表没有错误输入。
> # 定义一个空数据框用于存储最终结果
> result_df <- data.frame()
> 
> # 定义实际操作的数据集
> data_A <- "1.MR_GCST90255375.csv"
> data_B <- "2.MR_R10_K11_ACUTPANC.csv"
> A_Cases <- 10630
> A_Noncases <- 844679
> B_Cases <- 6787
> B_Noncases <- 361641
> 
> # 循环遍历每个exposure值,从对应的CSV文件中提取数据
> for (i in 1:length(exposure_vals)) {
+   
+   exposure_val <- exposure_vals[i]
+   
+   # 从GCST90255375.csv文件中提取数据
+   gcst_df <- read_csv(data_A)
+   
+   gcst_df <- gcst_df %>%
+     filter(id.exposure == exposure_val,
+            grepl("Inverse variance weighted", method))
+   
+   gcst_df <- dplyr::select(gcst_df, or, or_lci95, or_uci95, pval)
+   gcst_df <- rename(gcst_df,c(pval="P"))
+   gcst_df <- rename(gcst_df,c(or="OR"))
+   gcst_df$Exposure <- exposure_val
+   gcst_df$Study <- sub(".csv", "",sub("1.MR_", "", data_A))
+   gcst_df$Cases <- A_Cases
+   gcst_df$Noncases <- A_Noncases
+   
+   # 从R10_K11_ACUTPANC.csv文件中提取数据
+   r10_df <- read_csv(data_B)
+   r10_df <- r10_df %>%
+     filter(id.exposure == exposure_val,
+            grepl("Inverse variance weighted", method))
+   r10_df <- dplyr::select(r10_df, or, or_lci95, or_uci95, pval)
+   r10_df <- rename(r10_df,c(pval="P"))
+   r10_df <- rename(r10_df,c(or="OR"))
+   r10_df$Exposure <- exposure_val
+   r10_df$Study <- sub(".csv", "",sub("2.MR_", "", data_B))
+   r10_df$Cases <- B_Cases
+   r10_df$Noncases <- B_Noncases
+   
+   # 将两个表格合并成一个数据框
+   # temp_df <- bind_rows(gcst_df, r10_df)
+   temp_df <- merge(gcst_df, r10_df,all =TRUE)
+   # 将当前exposure值对应的结果添加到最终结果数据框中
+   # result_df <- bind_rows(result_df, temp_df)
+   result_df <- merge(result_df, temp_df,all =TRUE)
+ }  
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 187 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 212 Columns: 15                                                                                       
── Column specification ────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): id.exposure, id.outcome, outcome, exposure, method
dbl (10): nsnp, b, se, pval, lo_ci, up_ci, or, or_lci95, or_uci95, p_fdr

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
> 
> # 输出结果到Excel文件中
> write.xlsx(result_df, "meta阳性结果提取、合并(自动化)1.xlsx")
> 


3、源表中对应的”GCST90274765“行:

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/f57ada8b839f4df7a53399c2b8ed3775.png "#left")


![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/9f7502089f004949a844292e0ab64b8a.png "#left")
  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-06-03 15:35
    关注

    引自免费微信小程序:皆我百晓生

    在您的代码中,问题可能出在合并(merge)操作上。由于您在每次循环时都对result_df进行了更新,而没有先保存前一次循环的结果,这可能导致了第一个循环的值丢失。为了解决这个问题,您应该在循环外部初始化temp_df并累积结果,而不是每次都从头开始创建。

    这里是修复后的代码:

    # 初始化一个空数据框用于存储临时结果
    temp_df <- data.frame()
    
    # 定义实际操作的数据集
    data_A <- "1.MR_GCST90255375.csv"
    data_B <- "2.MR_R10_K11_ACUTPANC.csv"
    A_Cases <- 10630
    A_Noncases <- 844679
    B_Cases <- 6787
    B_Noncases <- 361641
    
    # 循环遍历每个exposure值,从对应的CSV文件中提取数据
    for (i in 1:length(exposure_vals)) {
    
      exposure_val <- exposure_vals[i]
    
      # 从GCST90255375.csv文件中提取数据
      gcst_df <- read_csv(data_A)
    
      gcst_df <- gcst_df %>%
        filter(id.exposure == exposure_val,
               grepl("Inverse variance weighted", method))
    
      gcst_df <- dplyr::select(gcst_df, or, or_lci95, or_uci95, pval)
      gcst_df <- rename(gcst_df,c(pval="P"))
      gcst_df <- rename(gcst_df,c(or="OR"))
      gcst_df$Exposure <- exposure_val
      gcst_df$Study <- sub(".csv", "",sub("1.MR_", "", data_A))
      gcst_df$Cases <- A_Cases
      gcst_df$Noncases <- A_Noncases
    
      # 从R10_K11_ACUTPANC.csv文件中提取数据
      r10_df <- read_csv(data_B)
      r10_df <- r10_df %>%
        filter(id.exposure == exposure_val,
               grepl("Inverse variance weighted", method))
      r10_df <- dplyr::select(r10_df, or, or_lci95, or_uci95, pval)
      r10_df <- rename(r10_df,c(pval="P"))
      r10_df <- rename(r10_df,c(or="OR"))
      r10_df$Exposure <- exposure_val
      r10_df$Study <- sub(".csv", "",sub("2.MR_", "", data_B))
      r10_df$Cases <- B_Cases
      r10_df$Noncases <- B_Noncases
    
      # 将两个表格合并成一个数据框
      temp_df <- rbind(temp_df, bind_rows(gcst_df, r10_df))
    }  
    
    # 将累积的temp_df写入最终结果数据框中
    result_df <- temp_df
    
    # 输出结果到Excel文件中
    write.xlsx(result_df, "meta阳性结果提取、合并(自动化)1.xlsx")
    

    这个修改过的代码应该可以正确地合并所有循环中的结果,包括第一个循环的值。请尝试运行这段代码,并检查生成的Excel文件是否包含所有预期的结果。如果有任何问题或需要进一步的帮助,请随时告诉我。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月11日
  • 已采纳回答 6月3日
  • 创建了问题 6月3日