我想使用数据框创建特定的成对比较矩阵。为此,我有:
数据框架包含5种产品(deltametrina,fipronil,imidaclopride,sulfluramida,tiametoxam)和2种产品(Indv)
Indv<-c(1,2)
deltametrina<-c(1,1)
fipronil<-c(5,3)
imidaclopride<-c(7,5)
sulfluramida<-c(3,7)
tiametoxam<-c(9,9)
DF<-cbind(Indv,deltametrina,fipronil,imidaclopride,sulfluramida,tiametoxam)
DF
之后,我创建了成对比较矩阵,我的规则是具有最高值的变量减去每个变量的较小数字数。但是列表对象中每个人(Indv)的最终矩阵。
df <- as.data.frame(t(DF[, -1]))
out <- lapply(df, function(x) outer(x, x, function(x, y) abs(x-y)))
out2 <- lapply(out, function(m) {
dimnames(m) <- list(rownames(df), rownames(df))
m
})
Finaly, 我以特定格式订购了矩阵:
out3 = list()
for(i in seq(1,length(out2)))
{
out3[[i]]=as.vector(out2[[i]])
names=
paste(
rep(rownames(out2[[i]]),each=ncol(out2[[i]])),
rep(colnames(out2[[i]]),nrow(out2[[i]]))
,sep='_')
names(out3[[i]])=names
}
[[1]]
deltametrina_deltametrina deltametrina_fipronil deltametrina_imidaclopride
0 4 6
deltametrina_sulfluramida deltametrina_tiametoxam fipronil_deltametrina
2 8 4
fipronil_fipronil fipronil_imidaclopride fipronil_sulfluramida
0 2 2
fipronil_tiametoxam imidaclopride_deltametrina imidaclopride_fipronil
4 6 2
imidaclopride_imidaclopride imidaclopride_sulfluramida imidaclopride_tiametoxam
0 4 2
sulfluramida_deltametrina sulfluramida_fipronil sulfluramida_imidaclopride
2 2 4
sulfluramida_sulfluramida sulfluramida_tiametoxam tiametoxam_deltametrina
0 6 8
tiametoxam_fipronil tiametoxam_imidaclopride tiametoxam_sulfluramida
4 2 6
tiametoxam_tiametoxam
0
[[2]]
deltametrina_deltametrina deltametrina_fipronil deltametrina_imidaclopride
0 2 4
deltametrina_sulfluramida deltametrina_tiametoxam fipronil_deltametrina
6 8 2
fipronil_fipronil fipronil_imidaclopride fipronil_sulfluramida
0 2 4
fipronil_tiametoxam imidaclopride_deltametrina imidaclopride_fipronil
6 4 2
imidaclopride_imidaclopride imidaclopride_sulfluramida imidaclopride_tiametoxam
0 2 4
sulfluramida_deltametrina sulfluramida_fipronil sulfluramida_imidaclopride
6 4 2
sulfluramida_sulfluramida sulfluramida_tiametoxam tiametoxam_deltametrina
0 2 8
tiametoxam_fipronil tiametoxam_imidaclopride tiametoxam_sulfluramida
6 4 2
tiametoxam_tiametoxam
0
但是,我想创建一个没有零的最终matriz和双重比较,如:
[[1]]
- [deltametrina, fipronil, 4]
- [deltametrina, imidaclopride, 6]
- [deltametrina, sulfluramida, 2]
- [deltametrina, tiametoxam, 8]
- [fipronil, imidaclopride, 2]
- [fipronil, sulfluramida, 2]
- [fipronil, tiametoxam, 4]
- [imidaclopride, sulfluramida, 4]
- [imidaclopride, tiametoxam, 2]
- [sulfluramida, tiametoxam, 6]
[[2]]
- [deltametrina, fipronil, 2]
- [deltametrina, imidaclopride, 4]
- [deltametrina, sulfluramida, 6]
- [deltametrina, tiametoxam, 8]
- [fipronil, imidaclopride, 2]
- [fipronil, sulfluramida, 4]
- [fipronil, tiametoxam, 6]
- [imidaclopride, sulfluramida, 2]
- [imidaclopride, tiametoxam, 4]
- [sulfluramida, tiametoxam, 2]
这可能吗?谢谢
答案 0 :(得分:1)
下面给出了数据框列表。每组有20行,但您的示例结果只显示10.我不知道应用什么规则来减少到10.我不太适合矩阵,所以我不确定最终的矩阵结构应该是什么是。
library (reshape2) # for the melt function to make wide data frame narrow
library(dplyr) # for simple sort and filter
result <- lapply(out2, function(x) {
melt(x) %>% filter(value!=0) %>% arrange(Var1, Var2)})
> result
$V1
Var1 Var2 value
1 deltametrina fipronil 4
2 deltametrina imidaclopride 6
3 deltametrina sulfluramida 2
4 deltametrina tiametoxam 8
5 fipronil deltametrina 4
6 fipronil imidaclopride 2
7 fipronil sulfluramida 2
8 fipronil tiametoxam 4
9 imidaclopride deltametrina 6
10 imidaclopride fipronil 2
11 imidaclopride sulfluramida 4
12 imidaclopride tiametoxam 2
13 sulfluramida deltametrina 2
14 sulfluramida fipronil 2
15 sulfluramida imidaclopride 4
16 sulfluramida tiametoxam 6
17 tiametoxam deltametrina 8
18 tiametoxam fipronil 4
19 tiametoxam imidaclopride 2
20 tiametoxam sulfluramida 6
$V2
Var1 Var2 value
1 deltametrina fipronil 2
2 deltametrina imidaclopride 4
3 deltametrina sulfluramida 6
4 deltametrina tiametoxam 8
5 fipronil deltametrina 2
6 fipronil imidaclopride 2
7 fipronil sulfluramida 4
8 fipronil tiametoxam 6
9 imidaclopride deltametrina 4
10 imidaclopride fipronil 2
11 imidaclopride sulfluramida 2
12 imidaclopride tiametoxam 4
13 sulfluramida deltametrina 6
14 sulfluramida fipronil 4
15 sulfluramida imidaclopride 2
16 sulfluramida tiametoxam 2
17 tiametoxam deltametrina 8
18 tiametoxam fipronil 6
19 tiametoxam imidaclopride 4
20 tiametoxam sulfluramida 2
答案 1 :(得分:1)
您也可以尝试使用out2
):
library(reshape2)
lapply(out2, function(x) {x <- as.matrix(x);
x[lower.tri(x, diag=FALSE)] <- 0;
df <- subset(melt(x), value>0);
df[order(df$Var1),]})
获取输出:
$V1
Var1 Var2 value
6 deltametrina fipronil 4
11 deltametrina imidaclopride 6
16 deltametrina sulfluramida 2
21 deltametrina tiametoxam 8
12 fipronil imidaclopride 2
17 fipronil sulfluramida 2
22 fipronil tiametoxam 4
18 imidaclopride sulfluramida 4
23 imidaclopride tiametoxam 2
24 sulfluramida tiametoxam 6
$V2
Var1 Var2 value
6 deltametrina fipronil 2
11 deltametrina imidaclopride 4
16 deltametrina sulfluramida 6
21 deltametrina tiametoxam 8
12 fipronil imidaclopride 2
17 fipronil sulfluramida 4
22 fipronil tiametoxam 6
18 imidaclopride sulfluramida 2
23 imidaclopride tiametoxam 4
24 sulfluramida tiametoxam 2