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/cn.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灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP