Re: [問題] while loop 不知道怎麼寫
大致上這樣做。
## site 座標
location <-
data.frame(
site.name = c("site1", "site2", "site3"),
x = c(3,1,16),
row.names = 1
)
## site 距離矩陣
dist.mat <-
dist(as.matrix(location)) %>%
as.matrix
dist.mat
## 各site物種出現豐度
spp.dt <-
data.frame(
spp = c("A", "B"),
site1 = c(5,4),
site2 = c(0,7),
site3 = c(20,2)
)
library(magrittr)
spp.dt[, c("site1", "site2", "site3")] %>%
as.matrix %>%
apply(., 1, function(y) {
y[y != 0] %>%
names(.) %>%
combn(., 2) %>%
apply(., 2, function(pair) {
dist.mat[pair[1], pair[2]]
}) %>%
max(.)
})
※ 引述《lilian0330 (俐俐)》之銘言:
: 大家好,剛開始統計軟體入門,寫過Matlab跟R
: 但最近在處理資料常常不知道怎麼解決error...
: [問題]
: 要做物種出現的「最大距離」,例如有三個地點,在三個點之間有三個不相等的距離。
: 而物種A出現在site1跟site3,所以距離就是distance(1-3)
: 物種B出現在site1,2,3,但因為2跟3比較遠,最大距離是distance(2-3)
: 我目前已經做出來兩兩site之間的distance matrix
: 但是我不知道用while迴圈怎麼讓R自動判斷這個物種的「最大距離」
: 資料形式
: Species| site1 | site2 | site3 | max_distance
: -------------------------------------------------------
: A | 5 | 0 | 20 | distance(1-3)
: B | 4 | 7 | 2 | distance(2-3)
: 我目前想到的邏輯是
: table[1,2]-table[1,3]!=table[1,2]|table[1,3]
: 依序做成這個物種的「所有距離」,最後再用max()選擇「最大距離」
: 想問問大家如果寫成while loop怎麼寫比較好呢?
: [環境敘述]:
: R version 3.3.2 (2016-10-31)
: Platform: x86_64-w64-mingw32/x64 (64-bit)
: Running under: Windows >= 8 x64 (build 9200)
: locale:
: [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese
: (Traditional)_Taiwan.950
: [3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C
: [5] LC_TIME=Chinese (Traditional)_Taiwan.950
: attached base packages:
: [1] stats graphics grDevices utils datasets methods base
: other attached packages:
: [1] sp_1.2-4 phyloseq_1.19.1 BiocInstaller_1.24.0
: readxl_0.1.1 ggplot2_2.2.1
: [6] vegan3d_1.0-1 vegan_2.4-2 lattice_0.20-34
: permute_0.9-4
: loaded via a namespace (and not attached):
: Error in x[["Version"]] : subscript out of bounds
: In addition: Warning messages:
: 1: In FUN(X[[i]], ...) :
: DESCRIPTION file of package 'Rcpp' is missing or broken
: 2: In FUN(X[[i]], ...) :
: DESCRIPTION file of package 'jsonlite' is missing or broken
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.135.110.74
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1491299743.A.AAF.html
→
04/04 21:16, , 1F
04/04 21:16, 1F
推
04/04 21:46, , 2F
04/04 21:46, 2F
→
04/04 21:46, , 3F
04/04 21:46, 3F
→
04/04 21:46, , 4F
04/04 21:46, 4F
→
04/04 22:00, , 5F
04/04 22:00, 5F
→
04/04 22:01, , 6F
04/04 22:01, 6F
→
04/04 22:03, , 7F
04/04 22:03, 7F
推
04/04 22:06, , 8F
04/04 22:06, 8F
→
04/04 22:07, , 9F
04/04 22:07, 9F
→
04/04 22:08, , 10F
04/04 22:08, 10F
→
04/04 22:21, , 11F
04/04 22:21, 11F
推
04/04 22:24, , 12F
04/04 22:24, 12F
→
04/04 22:24, , 13F
04/04 22:24, 13F
推
04/04 22:26, , 14F
04/04 22:26, 14F
→
04/04 22:28, , 15F
04/04 22:28, 15F
→
04/04 22:29, , 16F
04/04 22:29, 16F
→
04/04 22:30, , 17F
04/04 22:30, 17F
推
04/04 22:32, , 18F
04/04 22:32, 18F
推
04/04 22:34, , 19F
04/04 22:34, 19F
→
04/04 22:34, , 20F
04/04 22:34, 20F
→
04/04 22:35, , 21F
04/04 22:35, 21F
推
04/04 22:37, , 22F
04/04 22:37, 22F
推
04/04 23:01, , 23F
04/04 23:01, 23F
→
04/04 23:09, , 24F
04/04 23:09, 24F
location <-
data.frame(
site.name = c("site1", "site2", "site3"),
x = c(3, 1, 16),
row.names = 1
)
dist.mat <-
dist(as.matrix(location)) %>%
as.matrix
dist.mat
spp.dt <-
data.frame(
spp = c("A", "B", "C"),
site1 = c(5, 4, 0),
site2 = c(0, 7, 0),
site3 = c(20, 2, 1)
)
library(magrittr)
spp.dt[, c("site1", "site2", "site3")] %>%
as.matrix %>%
apply(., 1, function(y) {
if (sum(y >= 2)) { # 處理某 spp 只在 1 個以下 site 出現
y[y != 0] %>%
names(.) %>%
combn(., 2) %>%
apply(., 2, function(pair) {
dist.mat[pair[1], pair[2]]
}) %>%
max(.)
} else {
0
}
})
※ 編輯: andrew43 (125.230.106.85), 04/04/2017 23:10:56
※ 編輯: andrew43 (125.230.106.85), 04/04/2017 23:17:33
推
04/05 01:17, , 25F
04/05 01:17, 25F
→
04/05 01:17, , 26F
04/05 01:17, 26F
推
04/05 01:19, , 27F
04/05 01:19, 27F
→
04/05 01:22, , 28F
04/05 01:22, 28F
→
04/05 01:23, , 29F
04/05 01:23, 29F
→
04/05 01:24, , 30F
04/05 01:24, 30F
→
04/05 01:24, , 31F
04/05 01:24, 31F
推
04/05 01:32, , 32F
04/05 01:32, 32F
→
04/05 01:32, , 33F
04/05 01:32, 33F
討論串 (同標題文章)
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章