簡體   English   中英

在“ for循環”(R)中編寫表格

[英]write a table in “for loop” (R)

我在一個文本文件中有很多序列。 我使用“ read.fasta”功能導入這些序列。 我使用“ for循環”為每個序列創建核苷酸頻率表,並使用“ write.table”獲得輸出。 但是它會為每個序列創建一個文件(許多輸出文件,每個文件都有一個序列表)。 我搜索一個命令來創建一個包含所有表的文件。
注意:“ mydata.txt”是一個文件,包含許多具有fasta格式的序列

file=read.fasta(file="mydata") 
for (i in seq_along(file))
{
NucleotidesFrequency=table(file[[i]])
print(NucleotidesFrequency)
write.table(NucleotidesFrequency, paste(i, (".txt"), sep=""),sep="\t") 
}

您可以嘗試rbind (有關詳細信息,請參見?rbind ):

## create example data
set.seed(1)
s <- list(sample(LETTERS[1:4], size=20, replace=T), sample(LETTERS[1:4], size=20, replace=T))

## call table for each item in the list
l <- lapply(s, table)
l
#[[1]]
#A B C D 
#4 5 5 6 
#[[2]]
#A B C D 
#5 7 4 4

## using rbind to create a matrix
d <- do.call(rbind, l)
d
#    A B C D
#[1,] 4 5 5 6
#[2,] 5 7 4 4

## write it into a file
write.table(d, "file.txt")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM