作者lilian0330 (俐俐)
标题Re: [问题] while loop 不知道怎麽写
时间Wed Apr 5 01:46:17 2017
一部分原始资料
## Distance matrix #实际上有十几个站位
COM01 AR01 PA01 PA02
COM01 0 8 112 110
AR01 8 0 118 116
PA01 112 118 0 4
PA02 110 116 4 0
## spp matrix #实际上有十万多个物种
COM01 AR01 PA01 PA02
OTU1 7774 4 310 12
OTU2 5 0 81 9
OTU3 0 0 71 1
OTU4 1 0 0 0
把error的资讯跟traceback留在下面:
Error in dist.mat[pair[1], pair[2]] : subscript out of bounds
19.
FUN(newX[, i], ...)
18.
apply(., 2, function(pair) {
dist.mat[pair[1], pair[2]]
})
17.
function_list[[i]](value)
16.
freduce(value, `_function_list`)
15.
`_fseq`(`_lhs`)
14.
eval(expr, envir, enclos)
13.
eval(quote(`_fseq`(`_lhs`)), env, env)
12.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
11.
y[y != 0] %>% names(.) %>% combn(., 2) %>% apply(., 2, function(pair) {
dist.mat[pair[1], pair[2]]
}) %>% max(.)
10.
FUN(newX[, i], ...)
9.
apply(., 1, function(y) {
if (sum(y >= 2)) {
y[y != 0] %>% names(.) %>% combn(., 2) %>% apply(., 2,
function(pair) { ...
8.
function_list[[k]](value)
7.
withVisible(function_list[[k]](value))
6.
freduce(value, `_function_list`)
5.
`_fseq`(`_lhs`)
4.
eval(expr, envir, enclos)
3.
eval(quote(`_fseq`(`_lhs`)), env, env)
2.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
1.
OTU_location[, c("COM01", "AR01", "PA01", "PA02", "GJ01", "QK01",
"AK03", "SH01", "SI02", "TA01", "GG01", "DSZ01", "SM22",
"LGH03")] %>% as.matrix %>% apply(., 1, function(y) {
if (sum(y >= 2)) { ...
※ 引述《andrew43 (讨厌有好心推文後删文者)》之铭言:
: 大致上这样做。
: ## 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), 来自: 140.112.69.189
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1491327979.A.06C.html
2F:→ andrew43: 改用for loop写,慢很多但够应付了。 04/05 05:37
3F:→ andrew43: 会额外回传最远的二个site名 04/05 05:38
4F:→ andrew43: 建议你还是提何「可重覆错误的确实资料与R code」不然 04/05 05:40
5F:→ andrew43: 一直隔空抓药会浪费彼此时间。 04/05 05:40
6F:→ andrew43: s/提何/提供/ 04/05 05:41