私はRで新しいループです。複数のネストループを書くのに助けが必要です。私は行が1つの領域内の1つのサイトからの種の数を表すデータフレームを持っています。 50の地域があり、地域間のサイト数は不平等です。各地域では、サイト数を増やすことに基づいて多様性指標を計算し、増分ステップごとに1000xを複製する必要があります。例えば:ここでサンプルサイズを再サンプリングして増加させる入れ子になったループを書く方法
R1 <- subset(df, region=="1") #this needs to be completed for all 50 regions
R1$region<-NULL
max<-nrow(R1)-1
iter <- 1000 #the number of iterations
n <- 1 # the number of rows to be sampled. This needs to increase until
“max”
outp <- rep(NA, iter)
for (i in 1:iter){
d <- sample(1:nrow(R1), size = n, replace=FALSE)
bootdata <- R1[d,]
x <- colSums(bootdata) #this is not applicable until n>1
outp[i] <- 1/diversity(x, index = "simpson")
}
は、要するに、サンプルデータセット
structure(list(region = c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 4L), Sp1 = c(31L,
85L, 55L, 71L, 81L, 22L, 78L, 64L), Sp2 = c(10L, 84L, 32L, 86L,
47L, 93L, 55L, 35L), Sp3 = c(86L, 56L, 4L, 8L, 55L, 47L, 51L,
95L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L), .Names = c("region", "Sp1", "Sp2", "Sp3"), spec = structure(list(
cols = structure(list(region = structure(list(), class =
c("collector_integer",
"collector")), Sp1 = structure(list(), class = c("collector_integer",
"collector")), Sp2 = structure(list(), class = c("collector_integer",
"collector")), Sp3 = structure(list(), class = c("collector_integer",
"collector"))), .Names = c("region", "Sp1", "Sp2", "Sp3")),
default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
で、私は各サイトの「シンプソン」指数を計算する必要がある地域ごとに、ランダムに1000倍をリサンプリング。次に、各列が合計された後、1000回、2つのサイトのインデックスを再度計算する必要があります。最大3つのサイトなど
私も出力を書くのに苦労します。私は最大1000までの反復を表す列を持つ各領域のための1つのデータフレームを持つことを望んでいます。
多くのことに感謝します。
小さな再現性のあるデータセットを提供してください。 https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –
申し訳ありませんが、文字の制限に合わせて簡潔にデータセットを追加する方法を理解するのは本当に苦労しています –
@lmo元の投稿に追加しました。これは正しい形式です、はい?私はそれを基にしていますhttps://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –