使用 d_ply 写入多个自定义文件

发布于 2024-12-28 22:06:17 字数 1343 浏览 0 评论 0原文

这个问题与上一个问题几乎相同,但差异很大,该问题的答案在这里不起作用。就像上一个问题中的@chase一样,我想以以下格式(自定义fasta)为数据帧的每个分割写出多个文件。

#same df as last question

df <- data.frame(
    var1 = sample(1:10, 6, replace = TRUE)
    , var2 = sample(LETTERS[1:2], 6, replace = TRUE)
    , theday = c(1,1,2,2,3,3)
)    

#how I want the data to look
write(paste(">", df$var1,"_", df$var2, "\n", df$theday, sep=""), file="test.txt")

#whole df output looks like this:
#test.txt
>1_A 
1
>8_A
1
>4_A 
2
>9_A 
2
>2_A 
3
>1_A 
3

但是,我不想从整个数据帧获取输出,而是想为每个数据子集生成单独的文件。使用 d_ply 如下:

d_ply(df, .(theday), function(x) write(paste(">", df$var1,"_", df$var2, "\n", df$theday, sep=""), file=paste(x$theday,".fasta",sep="")))

我收到以下输出错误:

Error in file(file, ifelse(append, "a", "w")) : 
  invalid 'description' argument
In addition: Warning messages:
1: In if (file == "") file <- stdout() else if (substring(file, 1L,  :
  the condition has length > 1 and only the first element will be used
2: In if (substring(file, 1L, 1L) == "|") { :
  the condition has length > 1 and only the first element will be used

有关如何解决此问题的任何建议?

谢谢, 扎赫CP

This question is almost the same as a previous question, but differs enough that the answers for that question don't work here. Like @chase in the last question, I want to write out multiple files for each split of a dataframe in the following format(custom fasta).

#same df as last question

df <- data.frame(
    var1 = sample(1:10, 6, replace = TRUE)
    , var2 = sample(LETTERS[1:2], 6, replace = TRUE)
    , theday = c(1,1,2,2,3,3)
)    

#how I want the data to look
write(paste(">", df$var1,"_", df$var2, "\n", df$theday, sep=""), file="test.txt")

#whole df output looks like this:
#test.txt
>1_A 
1
>8_A
1
>4_A 
2
>9_A 
2
>2_A 
3
>1_A 
3

However, instead of getting the output from the entire dataframe I want to generate individual files for each subset of data. Using d_ply as follows:

d_ply(df, .(theday), function(x) write(paste(">", df$var1,"_", df$var2, "\n", df$theday, sep=""), file=paste(x$theday,".fasta",sep="")))

I get the following output error:

Error in file(file, ifelse(append, "a", "w")) : 
  invalid 'description' argument
In addition: Warning messages:
1: In if (file == "") file <- stdout() else if (substring(file, 1L,  :
  the condition has length > 1 and only the first element will be used
2: In if (substring(file, 1L, 1L) == "|") { :
  the condition has length > 1 and only the first element will be used

Any suggestions on how to get around this?

Thanks,
zachcp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2025-01-04 22:06:17

您的代码有两个问题。

  • 首先,在构造文件名时,您将向量 x$theday 传递给 paste()。由于 x$theday 取自 data.frame 的一列,因此它通常具有多个元素。当您将多个文件名传递给其 file= 参数时,您看到的错误是 write() 抱怨。使用 unique(x$theday) 可确保您只会粘贴一个文件名,而不是多个文件名。

  • 其次,您还没有走得足够远,无法看到它,但您可能想写入 x 的内容(data.frame 的当前子集),而不是整个内容df 到每个文件。

这是更正后的代码,看起来工作得很好。

d_ply(df, .(theday), 
    function(x) {write(paste(">", x$var1,"_", x$var2, "\n", x$theday, sep=""), 
                       file=paste(unique(x$theday),".fasta",sep=""))
    })

There were two problems with your code.

  • First, in constructing the file name, you passed the vector x$theday to paste(). Since x$theday is taken from a column of a data.frame, it often has more than one element. The error you saw was write() complaining when you passed several file names to its file= argument. Using instead unique(x$theday) ensures that you will only ever paste together a single file name rather than possibly more than one.

  • Second, you didn't get far enough to see it, but you probably want to write the contents of x (the current subset of the data.frame), rather than the entire contents of df to each file.

Here is the corrected code, which appears to work just fine.

d_ply(df, .(theday), 
    function(x) {write(paste(">", x$var1,"_", x$var2, "\n", x$theday, sep=""), 
                       file=paste(unique(x$theday),".fasta",sep=""))
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文