R_Language 板


LINE

大致上這樣做。 ## 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://webptt.com/m.aspx?n=bbs/R_Language/M.1491299743.A.AAF.html
1F:→ lilian0330: 感謝!我研究一下! 04/04 21:16
2F:推 lilian0330: 我跑完了可以執行,但換成原始資料後遇到一個問題 04/04 21:46
3F:→ lilian0330: geodist[pair[1], pair[2]] : no 'dimnames' attribut 04/04 21:46
4F:→ lilian0330: for array 04/04 21:46
5F:→ andrew43: 你比較我的資料matrix和你的看是不是少了欄名或列名 04/04 22:00
6F:→ andrew43: 真的解不開就直接提供你的部份資料。 04/04 22:01
7F:→ andrew43: 若你可以看懂code,相信這只是個小問題,修一下即可。 04/04 22:03
8F:推 lilian0330: 我找到了,距離矩陣沒有原本的樣本名字 04/04 22:06
9F:→ lilian0330: 但是因為我要用經緯度算,所以使用gdistance package 04/04 22:07
10F:→ lilian0330: 中的spDists(),只有在輸出成csv的時候是有site的 04/04 22:08
11F:→ andrew43: 那好辦,就多幫它補上即可。 04/04 22:21
12F:推 lilian0330: 謝謝你!我補上了 04/04 22:24
13F:→ lilian0330: 現在新的 Error in combn(., 2) : n < m 04/04 22:24
14F:推 lilian0330: m是2,n是什麼呀? 04/04 22:26
15F:→ andrew43: 你檢查是不是有某個site只有一個物種。應該是這個原因 04/04 22:28
16F:→ andrew43: 有的話,這段code要修以跳脫這種例外。 04/04 22:29
17F:→ andrew43: 說反了,是不是某個物種只出現在一個site。 04/04 22:30
18F:推 lilian0330: 喔喔有的,大概有70%以上~ 04/04 22:32
19F:推 lilian0330: 這種狀況distance定義=0 04/04 22:34
20F:→ andrew43: 把那些物種列都挑掉,或是改寫成if else 04/04 22:34
21F:→ andrew43: 下次把這種明顯的例外一起說明,別人才能一併處理。 04/04 22:35
22F:推 lilian0330: 好的> <抱歉提問的時候沒有想到這是特例...謝謝! 04/04 22:37
23F:推 lilian0330: if x = sum(x) else接原本的code?但我不確定x要改哪裡 04/04 23:01
24F:→ andrew43: 我再回一個 04/04 23:09
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
25F:推 lilian0330: 阿阿不行,出現 Error in dist.mat[pair[1], pair[2]] 04/05 01:17
26F:→ lilian0330: : subscript out of bounds 04/05 01:17
27F:推 lilian0330: 想詢問這邊pair的功能是什麼呢? 04/05 01:19
28F:→ andrew43: 你大概不懂apply怎麼用。把你一小部分資料貼出來吧。 04/05 01:22
29F:→ andrew43: 再看到底是你卡在哪裡。 04/05 01:23
30F:→ andrew43: 你把 dist.mat[pair[1], pair[2]] 改成 print(pair) 就 04/05 01:24
31F:→ andrew43: 可以看到pair實際上是什麼。 04/05 01:24
32F:推 lilian0330: 我看到pair了,但是三筆data有五個結果(site),不太懂 04/05 01:32
33F:→ andrew43: 那就是combn的結果,即所有site的兩兩組合。 04/05 01:32







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP