首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据列表名称创建文件夹并将列表保存到相应的文件夹中?

如何根据列表名称创建文件夹并将列表保存到相应的文件夹中?
EN

Stack Overflow用户
提问于 2021-01-31 08:19:51
回答 1查看 27关注 0票数 0

我的例子列表。所以它有大约3张桌子。所以现在,当我将这个列表保存到文件中时,我可以保存到单独的列表中。在我的列表中,这个ID是"PMC7837979“,例如,unique.The文件被保存为PMC7837979.Table1.txt,PMC7837979.Table2.txt等等。

所以现在,我的列表包含了一个小子集--示例6--唯一ID。在每个ID下有多个表。所以我的目标。

  • 为每个唯一ID生成一个文件夹,并在其中保存相应的ID,例如,如果"PMC7837979“有3个表,那么所有文件都应该位于PMC7837979命名文件夹?

中。

"PMC7837979“"PMC7809753”"PMC7790830“"PMC7797573”"PMC7806552“"PMC7836575”

代码语言:javascript
复制
dput(head(my_tables,1))
list(PMC7837979 = list(`Table 1` = structure(list(`Clinical feature` = c("Sex, n, Male/female", 
"Age, yr, Median (range)", "WBC × 109/L, Mean ± SD", 
"Course of CR", "One period of treatment, n", "Two periods of treatment, n", 
"Number of courses of consolidation, Median (range)"), `Intermediate dose group, n = 33` = c("15/18", 
"53 (22–73)", "25.3 ± 42.5", NA, "22", "11", "5 (3–7)"
), `Standard dose group, n = 19` = c("10/9", "56 (26–74)", 
"22.8 ± 36.8", NA, "11", "8", "7 (6–8)"), `P value` = c(".25", 
".23", ".52", NA, ".17", "", ".19")), row.names = c(NA, -7L), class = c("tbl_df", 
"tbl", "data.frame"), caption = "Clinical data of the 155 patients with acute myeloid leukaemia."), 
    `Table 2` = structure(list(subheading = c("3-year survival", 
    "3-year survival", "3-year survival", "3-year survival", 
    "3-year relapse-free survival", "3-year relapse-free survival", 
    "3-year relapse-free survival", "3-year relapse-free survival"
    ), Variable = c("Age", "Treatment strategy (Standard dose)", 
    "Risk grade", "WBC count", "Age", "Treatment strategy (Standard dose)", 
    "Risk grade", "WBC count"), HR = c(1.012, 2.302, 1.033, 0.891, 
    1.01, 2.23, 0.968, 1.002), `95% CI` = c("0.996–1.071", 
    "1.009–5.255", "0.3291–2.412", "0.997–1.027", "0.973–1.048", 
    "1.055–4.715", "0.362–2.594", "0.987–1.108"), `P value` = c(0.079, 
    0.048, 0.82, 0.126, 0.604, 0.036, 0.949, 0.759)), row.names = c(NA, 
    -8L), class = c("tbl_df", "tbl", "data.frame"), caption = "Multivarivate analysis by Cox Regression for survival and relapse-free of 3 year."), 
    `Table 3` = structure(list(subheading = c("Grade 3-4 hematological toxicity", 
    "Grade 3-4 hematological toxicity", "Grade 3-4 hematological toxicity", 
    "Grade 3-4 hematological toxicity", "Grade 3-4 hematological toxicity", 
    "Grade 3-4 hematological toxicity", "Non-hematological toxicity", 
    "Non-hematological toxicity", "Non-hematological toxicity"
    ), Group = c("Neutrophil recovery time (d), Mean ± SD", 
    "Minimum neutrophil time (d), Mean ± SD", "Duration of neutrophil deficiency (d), Mean ± SD", 
    "Platelet recovery time (d), Mean ± SD", "Minimum platelet time (d), Mean ± SD", 
    "Duration of thrombocytopenia (d), Mean ± SD", "Adverse gastrointestinal reactions, n", 
    "Blood transfusion, n", "Infection occurs, n"), `Intermediate dose group, n = 33` = c("15.5 ± 5.9", 
    "10 ± 5.8", "7.8 ± 10.7", "16 ± 5.3", "10.3 ± 2.2", 
    "9.5 ± 7.5", "31", "24", "25"), `Standard dose group, n = 19` = c("14 ± 2.4", 
    "9 ± 1.9", "7 ± 2", "14.5 ± 1.5", "9.4 ± 1.6", 
    "9 ± 2.1", "16", "11", "14"), `P value` = c(0.16, 0.09, 
    0.9, 0.29, 0.47, 0.36, 0.51, 0.43, 0.87)), row.names = c(NA, 
    -9L), class = c("tbl_df", "tbl", "data.frame"), caption = "Grade 3-4 hematological toxicity and non-hematological toxicity.")))
EN

回答 1

Stack Overflow用户

发布于 2021-01-31 12:53:21

以下是一个初步的基本R解决方案,因为我无法让您提供的数据工作。你能缩短你的数据,使它更容易插入R?

假设我们有以下模仿你的例子的玩具数据。

数据

代码语言:javascript
复制
df <- data.frame(var1 = c(1, 2), var2 = c(2, 3)) 
IDS <- list(A = list(df, df), B = list(df, df))
IDS 
> IDS
$A
$A[[1]]
  var1 var2
1    1    2
2    2    3

$A[[2]]
  var1 var2
1    1    2
2    2    3


$B
$B[[1]]
  var1 var2
1    1    2
2    2    3

$B[[2]]
  var1 var2
1    1    2
2    2    3

然后,您可以使用*apply系列,如下所示。

代码语言:javascript
复制
lapply(names(IDS), function(x) {dir.create(x); 
                    sapply(1:length(IDS[[x]]), function(y) {
                      write.table(IDS[[x]][y],  file = paste(x, "/",  paste(x, y, sep = "_"), 
                                                              ".txt", sep = ""))
                    })
})

代码使用dir.create()为列表中的每个列表创建一个文件夹,其名称基于整个list对象的names()。然后,它解压缩在各个文件夹的每个列表中找到的所有data.frame,并通过list和唯一的编号来命名它们。你就是这么想的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65977027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档