英文:
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<- list() ###Empty list for storing computed multinomial densities
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 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 0 1 0 0 1 1 1 0 0 0 2 1 1 1 1
[47] 2 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, 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 <- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论