【问题标题】:How to write csv file in R for following data?如何在 R 中为以下数据编写 csv 文件?
【发布时间】:2017-06-22 08:36:55
【问题描述】:

我想使用登录号获取获取分类层次结构。我发现最简单的方法是

library('taxize')
for (year in c("AY744148.1","AY656167.1","AY656168.1")){print(paste( year))}
classification(genbank2uid(id = year), db = "ncbi")

R 给我以下输出, 但是我无法从中写入 csv 文件

我想要 csv 文件中的结果,如下图所示 需要输出

请求您在这方面提供帮助 问候 阿里·佐海布

【问题讨论】:

  • 您将需要一个 for() 循环,或者可能需要 sapply 或 apply 系列中的其他一些函数。
  • 谢谢先生,for() 函数解决了输入数据的问题。现在我面临如何从输出中写入 csv 文件。我已经更新了我的问题

标签: r parsing export-to-csv taxonomy blast


【解决方案1】:

也许你正在追求这样的东西?

library('taxize')

IDs <- c("AY744148.1","AY656167.1","AY656168.1") # your sequence of ids to iterate over
output <- vector("list", length(years)) # prepare a list to write to

# for each ID, run the function and store the result into a list
for (i in 1:length(years)) {
  # output is a list of length one, so we need to subset it using "[["
  output[[i]] <- classification(genbank2uid(id = IDs[i]), db = "ncbi")[[1]]
  }

# take list elements of output and merge them row-wise (rbind == row bind)
output <- do.call(rbind, output)

                   name    rank    id
1        other sequences no rank 28384
2   artificial sequences no rank 81077
3                vectors no rank 29278
4  Cloning vector pBR322 species 47470
5        other sequences no rank 28384
6   artificial sequences no rank 81077
7                vectors no rank 29278
8  Cloning vector pGEM-3 species 90108
9        other sequences no rank 28384
10  artificial sequences no rank 81077
11               vectors no rank 29278
12 Cloning vector pGEM-3 species 90108

您可以使用

编写结果
write.table(output, file = "endfile.txt", row.names = FALSE, quote = FALSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2018-09-14
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多