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“行:

