gpt4 book ai didi

r - 将字符向量列表写入 R 中的单个文件

转载 作者:行者123 更新时间:2023-12-05 08:34:09 27 4
gpt4 key购买 nike

我有一个字符向量列表,我想将该列表写入包含所有字符向量的单个文件。这是我需要的示例:

>str(mylist)
List of 19
$ geneset1 : chr [1:140] "ASGR2" "ATXN7L3" "BCL6B" "C6orf211" ...
$ geneset2 : chr [1:174] "CKS1B" "CREBL2" "CTNNB1" "CTTN" ...
$ geneset3 : chr [1:346] "AGTR1" "C6" "C6orf211" "CCNK" ...
$ geneset4 : chr [1:259] "ASGR2" "ATF7IP" "ATXN7L3" "CKS1B" ...

我想要的输出是这样的文件:

#myfile
>geneset1
ASGR2 ATXN7L3 BCL6B C6orf211
>geneset2
ASGR2 ATXN7L3 BCL6B C6orf211
>geneset3
AGTR1 C6 C6orf211 CCNK

我的做法是这样的:

writeLines(unlist(lapply(mylist, FUN=function(x)paste(x, collapse=" "))), con="test.txt")

但是,我不知道如何在行首添加“>”

谢谢

最佳答案

您可以只使用 for 循环:

out=stdout()

X <- list(geneset1= c("ASGR2","ATXN7L3","BCL6B","C6orf211"),
geneset2= c("CKS1B","CREBL2","CTNNB1","CTTN"),
geneset3= c("AGTR1","C6","C6orf211","CCNK" ),
geneset4= c("ASGR2","ATF7IP","ATXN7L3","CKS1B" ) )

for(i in names(X)) {
cat(">", i, "\n", file=out)
cat(X[[i]], "\n", file=out)
}

关于r - 将字符向量列表写入 R 中的单个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32701021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com