【问题标题】:Looping several dataframes into individual csv files in R将多个数据帧循环到 R 中的单个 csv 文件中
【发布时间】:2021-10-24 21:58:36
【问题描述】:

编辑:添加了我的数据框的 sn-p

我正在尝试将一个数据帧拆分为几个较小的数据帧,然后将每个数据帧保存到一个带有主数据帧唯一标识符的 csv 文件中

metrics %>%  #main dataframe
  spread(base, pct ) %>% 
  select(sample_name,Cycle = pos,R1.A,R1.C,R1.G,R1.T,R1.N,R2.A) %>% 
  group_split(sample_name) %>%  #splitting it into several smaller dataframes based on sample names
  map(select, -sample_name) %>%  #excluding first column 
  iwalk( ~ write_csv(.x, str_c('/home/Projects/', '%s.csv')),a_id) # a_id is a column name in metrics dataframe 

我的文件在 /home/Projects/ 中保存为 %s.csv,而不是为每个 csv 文件使用来自 a_id 的唯一值,例如 2910968.csv2908963.csv 任何建议都会很有用。

我的主要数据框的小sn-p dput(metrics)

structure(list(b_id = c(163173, 163173, 163173, 163173, 163173, 
163173, 163173, 163173, 163173, 163173, 164172, 164172, 164172, 
164172, 164172, 164172, 164172, 164172, 164172, 164172), sample_name = c("Sample_1", 
"Sample_1", "Sample_1", "Sample_1", "Sample_1", "Sample_1", "Sample_1", 
"Sample_1", "Sample_1", "Sample_1", "Sample_2", "Sample_2", "Sample_2", 
"Sample_2", "Sample_2", "Sample_2", "Sample_2", "Sample_2", "Sample_2", 
"Sample_2"), a_id = c(2910968, 2910968, 2910968, 2910968, 2910968, 
2910968, 2910968, 2910968, 2910968, 2910968, 2908963, 2908963, 
2908963, 2908963, 2908963, 2908963, 2908963, 2908963, 2908963, 
2908963), type = c("basecomp", "basecomp", "basecomp", "basecomp", 
"basecomp", "basecomp", "basecomp", "basecomp", "basecomp", "basecomp", 
"basecomp", "basecomp", "basecomp", "basecomp", "basecomp", "basecomp", 
"basecomp", "basecomp", "basecomp", "basecomp"), pos = c(1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), base = c("R1.A", 
"R1.C", "R1.G", "R1.N", "R1.T", "R2.A", "R2.C", "R2.G", "R2.N", 
"R2.T", "R1.A", "R1.C", "R1.G", "R1.N", "R1.T", "R2.A", "R2.C", 
"R2.G", "R2.N", "R2.T"), pct = c(30.35095242, 20.80328011, 16.59966437, 
6.83e-05, 32.24603478, 31.28154795, 20.39882211, 17.25755071, 
0.005099781, 31.05697946, 30.16529478, 20.67986859, 16.16195464, 
7.86e-05, 32.99280343, 30.47328103, 20.18747421, 17.29746286, 
0.005100642, 32.03668127)), spec = structure(list(cols = list(
    b_id = structure(list(), class = c("collector_double", "collector"
    )), sample_name = structure(list(), class = c("collector_character", 
    "collector")), a_id = structure(list(), class = c("collector_double", 
    "collector")), type = structure(list(), class = c("collector_character", 
    "collector")), pos = structure(list(), class = c("collector_double", 
    "collector")), base = structure(list(), class = c("collector_character", 
    "collector")), pct = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x7f935a8c5f90>, row.names = c(NA, 
-20L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
))

谢谢

【问题讨论】:

  • 我已经编辑了帖子以添加数据框的 sn-p
  • 那几乎没有帮助。以随时可用的格式提供您的样本数据,例如带输入
  • 更新到 dput
  • 每个a_id 是否是唯一的sample_name,反之亦然?

标签: r loops csv dplyr tidyverse


【解决方案1】:

你可以试试这样的:

library(tidyverse)

metrics %>%  #main dataframe
  pivot_wider(names_from = base, values_from = pct) %>%
  select(sample_name, a_id, Cycle = pos, R1.A, R1.C, R1.G, R1.T, R1.N, R2.A) %>% 
  group_split(sample_name) %>%  #splitting it into several smaller dataframes based on sample names
  walk(
    ~.x %>% 
      { 
        a_id <- .x %>% select(a_id) %>% distinct() %>% pull()
        .x %>% 
          select(-sample_name, -a_id) %>% 
          write_csv(str_c("/home/Projects/", a_id, ".csv"))
      } 
  )

这应该在文件夹/home/Projects/ 中创建几个.csv 文件,每个文件都以a_id 命名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多