列表操作

huangapple go评论110阅读模式
英文:

List Opearation

问题

我正在尝试对列表中的项目执行循环操作,虽然看起来很简单,但由于无法检测到的错误,我没有得到预期的解决方案。代码如下:

multinoms <- list() ###用于存储计算的多项式密度的空列表
for (l in 1:length(sampled_geno)){
  for (i in 1:length(sampled_geno[1])){
    b <- as.data.frame(table(sampled_geno[i]))
    multinoms[l] <- dmultinom(x=c(b$Freq[1], b$Freq[2], b$Freq[3]), prob= c(b$Freq[1]/sum(b$Freq),b$Freq[2]/sum(b$Freq),b$Freq[3]/sum(b$Freq)))
  }
}

Sampled_geno是一个长度为5的列表。使用我的代码,我只得到一个计算出的密度,重复了5次,尽管列表的值是不同的,如下所示:

> sampled_geno
[[1]]
 [1] 1 0 2 0 1 1 0 1 1 1 2 2 1 1 0 0 2 0 1 1 0 0 1 1 1 2 1 0 0 0 2 0 1 0 0 1 1 1 0 0 0 2 1 1 1 1
[47] 2 2 1 2 0 2 1 1 1 0 1 1 1 2 0 0 0 0 1 1 1 0 0 0 1 2 0 2 0 0 0 0 0 1 1 1 0 2 1 0 1 0 1 1 0 1
[93] 0 2 1 1 1 0

[[2]]
 [1] 1 0 2 0 1 1 0 1 1 1 2 2 1 1 0 0 2 0 1 1 0 0 1 1 1 2 1 0 0 0 2 0 1 0 0 1 1 1 0 0 0 2 1 1 1 1
[47] 2 2 1 2 0 2 1 1 1 0 0 1 2 0 1 1 1 0 0 1 1 0 0 2 0 1 1 1 1 1 0 0 1 1 0 1 2 0 1 0 1 0 2 1 1 1
[93] 0 0 0 1 0 0

[[3]]
 [1] 1 0 2 0 1 1 0 1 1 1 2 2 1 1 0 0 2 0 1 1 0 0 1 1 1 2 1 0 0 0 2 0 1 0 0 1 1 1 0 0 0 2 1 1 1 1
[47] 2 2 1 2 0 2 1 1 1 0 2 0 1 1 1 1 0 1 0 1 2 0 0 1 0 1 0 2 1 2 1 1 1 1 0 1 0 1 2 1 1 2 1 1 1 1
[93] 1 0 0 0 1 1

[[4]]
 [1] 1 0 2 0 1 1 0 1 1 1 2 2 1 1 0 0 2 0 1 1 0 0 1 1 1 2 1 0 0 0 2 0 1 0 0 1 1 1 0 0 0 2 1 1 1 1
[47] 2 2 1 2 0 2 1 1 1 0 0 1 1 0 1 0 0 1 1 1 1 1 1 0 0 0 0 2 1 0 0 1 0 0 0 2 1 0 0 1 0 1 0 2 2 0
[93] 0 0 1 0 0 1

[[5]]
 [1] 1 0 2 0 1 1 0 1 1 1 2 2 1 1 0 0 2 0 1 1 0 0 1 1 1 2 1 0 0 0 2 0 1 0 0 1 1 1 0 0 0 2 1 1 1 1
[47] 2 2 1 2 0 2 1 1 1 0 1 0 2 1 1 0 2 0 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 1 0 2 1 1 0 2 0 1 0 2 2 0
[93] 0 0 0 1 1 1

非常感谢任何帮助。

英文:

I am trying to perform a loop operation on the items of a list and while it seeems simple enough, I am not getting the expected solution due to a bug I can't detect. the code is:

multinoms&lt;- list() ###Empty list for storing computed multinomial densities
for (l in 1:length(sampled_geno)){
for (i in 1:length(sampled_geno[1])){
b &lt;- as.data.frame(table(sampled_geno[i]))
multinoms[l] &lt;-dmultinom(x=c(b$Freq[1], b$Freq[2], b$Freq[3]), prob= c(b$Freq[1]/sum(b$Freq),b$Freq[2]/sum(b$Freq),b$Freq[3]/sum(b$Freq)))
}
}

Sampled_geno is a list of lenght 5. Using my code, I only get one computed density repeated 5 times although the list values are different as we see below.

[[1]]
[1] 1 0 2 0 1 1 0 1 1 1 2 2 1 1 0 0 2 0 1 1 0 0 1 1 1 2 1 0 0 0 2 0 1 0 0 1 1 1 0 0 0 2 1 1 1 1
[47] 2 2 1 2 0 2 1 1 1 0 1 1 1 2 0 0 0 0 1 1 1 0 0 0 1 2 0 2 0 0 0 0 0 1 1 1 0 2 1 0 1 0 1 1 0 1
[93] 0 2 1 1 1 0
[[2]]
[1] 1 0 2 0 1 1 0 1 1 1 2 2 1 1 0 0 2 0 1 1 0 0 1 1 1 2 1 0 0 0 2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1, 0, 0, 1, 1, 1, 2, 1, 0, 0, 0, 2, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 2, 1, 1, 1, 1, 2, 2, 1, 2, 0, 2, 1, 1, 1, 0, 1, 0, 2, 1, 1, 0, 2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 1, 1, 0, 2, 0, 1, 0, 2, 2, 0, 0, 0, 0, 1, 1, 1))

答案1

得分: 1

sampled_geno中,使用[[ ]]而不是[]来访问列表元素。

for (l in 1:length(sampled_geno)){
  b <- as.data.frame(table(sampled_geno[[l]]))
  multinoms[[l]] <- dmultinom(x=c(b$Freq[1], b$Freq[2], b$Freq[3]), prob= c(b$Freq[1]/sum(b$Freq),b$Freq[2]/sum(b$Freq),b$Freq[3]/sum(b$Freq)))
}
[[1]]
[1] 0.009551857

[[2]]
[1] 0.009774618

[[3]]
[1] 0.009624812

[[4]]
[1] 0.009718148

[[5]]
[1] 0.009416344
英文:

In sampled_geno use [[ ]] instead of [] to access the list element.

for (l in 1:length(sampled_geno)){
b &lt;- as.data.frame(table(sampled_geno[[l]]))
multinoms[[l]] &lt;- dmultinom(x=c(b$Freq[1], b$Freq[2], b$Freq[3]), prob= c(b$Freq[1]/sum(b$Freq),b$Freq[2]/sum(b$Freq),b$Freq[3]/sum(b$Freq)))
}
[[1]]
[1] 0.009551857
[[2]]
[1] 0.009774618
[[3]]
[1] 0.009624812
[[4]]
[1] 0.009718148
[[5]]
[1] 0.009416344

huangapple
  • 本文由 发表于 2023年8月9日 00:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定