require(vegan)
我的相异矩阵(100 * 100观测值)基于Chase等人提出的Raup-Crick度量。 (2011年)。这个矩阵应该有负面的差异 - 这不是问题所在,不要惊慌。
RC_2015 <- read.csv(file="RC_2015.csv")
9 9 9 12 12 12 12 12 12 12 12
9 -0.91 -1.00 -1.00 -0.99 -1.00 -0.99 -0.11 -0.08 -0.27 -0.16 -1.00
9 -0.90 -0.98 -1.00 -0.98 -1.00 -0.97 -0.80 -0.90 -0.86 -0.94 -0.99
9 -0.94 -1.00 -0.98 -0.96 -0.96 -0.99 -0.63 -0.21 -0.41 -0.31 -1.00
12 -0.94 -1.00 -0.99 -0.99 -1.00 -0.93 -0.65 0.52 0.09 -0.60 -1.00
12 -0.55 -0.98 -0.98 -0.99 -1.00 -1.00 -0.63 0.28 0.02 -0.31 -0.99
12 -0.82 -0.98 -0.98 -0.96 -0.99 -0.99 0.17 0.28 0.02 0.15 -0.99
12 -0.33 -0.50 0.05 -0.55 -0.82 0.07 0.09 0.97 0.86 0.66 -0.86
12 -0.63 -0.99 -0.92 -0.98 -1.00 -0.97 -0.36 0.51 0.32 -0.02 -1.00
12 -0.82 -0.98 -0.98 -0.86 -0.99 -0.97 -0.27 0.28 0.02 0.15 -0.99
12 -1.00 -0.97 -0.96 -0.51 -0.51 -0.59 0.32 0.73 -0.27 -0.16 -1.00
矩阵看起来像什么的片段
根据因素分组数据(在这种情况下,因子是年龄)
groups <- factor(c(rep(1,10), rep(2,10), rep(3,10), rep(4,10), rep(5,10), rep(6,10), rep(7,10), rep(8,10), rep(9,10), rep(10,10)), labels = c("5","9","12","15","19","23","27","31","35","38"),exclude = NULL)
我需要计算&#39;内的平均差异度。 &#39;介于&#39;之间组。这就是&#39; meandist&#39;功能进来。
meandist(dist, grouping...)
我一步一步地按照函数的代码,因为我在使用函数时收到错误消息。
dist<-RC_2015[,-1]
grouping <- groups
grow <- grouping[as.dist(row(as.matrix(dist)))]
gcol <- grouping[as.dist(col(as.matrix(dist)))]
first <- as.numeric(grow) >= as.numeric(gcol)
cl1 <- ifelse(first, grow, gcol)
cl2 <- ifelse(!first, grow, gcol)
n <- table(grouping)
take <- matrix(TRUE, nlevels(grouping), nlevels(grouping))
diag(take) <- n > 1
take[upper.tri(take)] <- FALSE
out <- matrix(NA, nlevels(grouping), nlevels(grouping))
## Get output matrix
tmp <- tapply(dist, list(cl1, cl2), mean)
out[take] <- tmp[!is.na(tmp)]
out[upper.tri(out)] <- t(out)[upper.tri(out)]
rownames(out) <- colnames(out) <- levels(grouping)
class(out) <- c("meandist", "matrix")
attr(out, "n") <- table(grouping)
out
错误消息
tmp <- tapply(dist, list(cl1, cl2), mean)
tapply错误(dist,list(cl1,cl2),mean): 参数必须具有相同的长度
如何避免此错误?