如何去掉“”对象名称和循环工作?

发布于 2024-12-06 07:13:38 字数 891 浏览 1 评论 0原文

我不知道如何准确定义这个问题;但我无法弄清楚这一点,

    genx <- list(scaffold_1 = c("AAATTTTTATAT"),scaffold_2 = c("AAATTTTTATAT"),
    scaffold_3 = c("AAATTTTTATAT"),scaffold_4 = c("AAATTTTTATAT"),
    scaffold_5 = c("AAATTTTTATATA"),scaffold_6 = c("AAATTTTTATAT"),
scaffold_7 = c("AAATTTTTATAT"),scaffold_8 = c("AAATTTTTATATA"))


TATA = "TATA"   
      myobs <- paste("genx$scaffold_", 1:8, sep = "")

我想将以下函数应用于 myobs 元素(是对象)的每个元素:

source("http://www.bioconductor.org/biocLite.R")

biocLite("Biostrings")
require((Biostrings)

countPattern (TATA, genx$scaffold_1, max.mismatch = 1)
[1] 3  

当我使用以下内容时:

countPattern (TATA, myobs[1], max.mismatch = 1)

不起作用,因为我相信解释为:

countPattern (TATA, "genx$scaffold_1", max.mismatch = 1)
 [1] 0 

与上面不同一。如何摆脱“”并创建一个循环来执行这项工作,感谢您的建议:

I do not know how to precisely define this problem; but I could not figure this out

    genx <- list(scaffold_1 = c("AAATTTTTATAT"),scaffold_2 = c("AAATTTTTATAT"),
    scaffold_3 = c("AAATTTTTATAT"),scaffold_4 = c("AAATTTTTATAT"),
    scaffold_5 = c("AAATTTTTATATA"),scaffold_6 = c("AAATTTTTATAT"),
scaffold_7 = c("AAATTTTTATAT"),scaffold_8 = c("AAATTTTTATATA"))


TATA = "TATA"   
      myobs <- paste("genx$scaffold_", 1:8, sep = "")

I want to apply the following function to every elments of the elements of myobs (are objects):

source("http://www.bioconductor.org/biocLite.R")

biocLite("Biostrings")
require((Biostrings)

countPattern (TATA, genx$scaffold_1, max.mismatch = 1)
[1] 3  

When I use the following:

countPattern (TATA, myobs[1], max.mismatch = 1)

Doesnot work as it is I believe interpreted as:

countPattern (TATA, "genx$scaffold_1", max.mismatch = 1)
 [1] 0 

Which is not same as the above one. How can get rid of "" and create a loop to perform this job, your suggestions are appreciated:

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

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

发布评论

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

评论(5

公布 2024-12-13 07:13:38

您可以直接使用列表并执行 sapply 操作,该操作作用于列表的每个元素。这是一些示例代码

sapply(genx, countPattern, pattern = TATA, max.mismatch = 1)

You can directly use the list and do an sapply which acts on every element of the list. Here is some sample code

sapply(genx, countPattern, pattern = TATA, max.mismatch = 1)
花海 2024-12-13 07:13:38

您可以使用get函数

a  <- 12
get("a") # returns 12

You can use the get function

a  <- 12
get("a") # returns 12
笛声青案梦长安 2024-12-13 07:13:38

这个怎么样:

sapply(genx, function (x) { countPattern(TATA, x, max.mismatch = 1) })

What about this:

sapply(genx, function (x) { countPattern(TATA, x, max.mismatch = 1) })
水晶透心 2024-12-13 07:13:38

有一个问题:

get("genx$scaffold_1")

因为 R 认为您正在寻找具有该全名的对象,而不是“genx”的“scaffold_1”组件。

应该有效的是:

eval(parse(text="genx$scaffold_1"))

但是请参阅:

fortune(106)

更多的麻烦点可以在“The R Inferno”中找到:http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

There is a problem with:

get("genx$scaffold_1")

because R thinks you are looking for an object with that full name, not the 'scaffold_1' component of 'genx'.

What should work is:

eval(parse(text="genx$scaffold_1"))

But see:

fortune(106)

More trouble spots along these lines can be found in 'The R Inferno': http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

人生戏 2024-12-13 07:13:38

我建议您将 genx$scaffold 定义为向量,然后您可以轻松使用 apply 函数:

genx = data.frame(scaffold = c(1, 2, 3, 4)) # genx$scaffold is a vector

现在您可以轻松地在每个 genx$ 上运行 countPattern() 函数脚手架的元素

apply(as.matrix(genx$scaffold), 1, 
    function (x) { countPattern(TATA, x, max.mismatch = 1) })

您可以轻松扩展解决方案,以便 genx$scaffold 也可以是矩阵等。

I would recommend that you define genx$scaffold as vector, and then you can easily use the apply function:

genx = data.frame(scaffold = c(1, 2, 3, 4)) # genx$scaffold is a vector

Now you can easily run the countPattern() function on every genx$scaffold's element

apply(as.matrix(genx$scaffold), 1, 
    function (x) { countPattern(TATA, x, max.mismatch = 1) })

You can easily extend the solution so that genx$scaffold can be also matrix etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文