在R中,如何使用combn检索完整的矩阵?

发布于 2024-10-31 09:04:32 字数 538 浏览 4 评论 0原文

我的问题,删除特定目的,看起来像这样: 如何转换这样的组合: 首先使用combn(letters[1:4], 2)计算组合

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a"  "a"  "a"  "b"  "b"  "c" 
[2,] "b"  "c"  "d"  "c"  "d"  "d" 

使用每一列获取另一个数据框:

   [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1    2    3    4   5     6

获取元素,例如:第一个元素,从第一列上面的数据框

然后我如何将上面的数据框转换为矩阵,例如结果,例如:

   a   b   c  d
a  0   1   2  3
b  1   0   4  5
c  2   4   0  6
d  3   5   6  0

具有相同列和行名称的元素将具有零值,而其他元素对应于上述值

My problem, removing the specific purpose, seems like this:
how to transform a combination like this:
first use combn(letters[1:4], 2) to calculate the combination

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a"  "a"  "a"  "b"  "b"  "c" 
[2,] "b"  "c"  "d"  "c"  "d"  "d" 

use each column to obtain another data frame:

   [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1    2    3    4   5     6

elements are obtained, for example: the first element, from the first column of the above dataframe

then How can i transform the above dataframe into a matrix, for example result, things like:

   a   b   c  d
a  0   1   2  3
b  1   0   4  5
c  2   4   0  6
d  3   5   6  0

the elements with same col and row names will have zero value where others corresponding to above value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2024-11-07 09:04:32

这是一种可行的方法:

inputs <- letters[1:4]
combs <- combn(inputs, 2)
N <- seq_len(ncol(combs))
nams <- unique(as.vector(combs))
out <- matrix(ncol = length(nams), nrow = length(nams))
out[lower.tri(out)] <- N
out <- t(out)
out[lower.tri(out)] <- N
out <- t(out)
diag(out) <- 0
rownames(out) <- colnames(out) <- inputs

给出:

> out
  a b c d
a 0 1 2 3
b 1 0 4 5
c 2 4 0 6
d 3 5 6 0

如果我必须经常这样做,我会将这些函数调用包装到一个函数中。

另一种选择是使用 as.matrix.dist() 通过手动设置 "dist" 对象来为我们进行转换。使用之前的一些对象:

## Far easier
out2 <- N
class(out2) <- "dist"
attr(out2, "Labels") <- as.character(inputs)
attr(out2, "Size") <- length(inputs)
attr(out2, "Diag") <- attr(out2, "Upper") <- FALSE
out2 <- as.matrix(out2)

这给出了:

> out2
  a b c d
a 0 1 2 3
b 1 0 4 5
c 2 4 0 6
d 3 5 6 0

再次,如果我必须多次执行此操作,我会将其包装在一个函数中。

Here is one way that works:

inputs <- letters[1:4]
combs <- combn(inputs, 2)
N <- seq_len(ncol(combs))
nams <- unique(as.vector(combs))
out <- matrix(ncol = length(nams), nrow = length(nams))
out[lower.tri(out)] <- N
out <- t(out)
out[lower.tri(out)] <- N
out <- t(out)
diag(out) <- 0
rownames(out) <- colnames(out) <- inputs

Which gives:

> out
  a b c d
a 0 1 2 3
b 1 0 4 5
c 2 4 0 6
d 3 5 6 0

If I had to do this a lot, I'd wrap those function calls into a function.

Another option is to use as.matrix.dist() to do the conversion for us by setting up a "dist" object by hand. Using some of the objects from earlier:

## Far easier
out2 <- N
class(out2) <- "dist"
attr(out2, "Labels") <- as.character(inputs)
attr(out2, "Size") <- length(inputs)
attr(out2, "Diag") <- attr(out2, "Upper") <- FALSE
out2 <- as.matrix(out2)

Which gives:

> out2
  a b c d
a 0 1 2 3
b 1 0 4 5
c 2 4 0 6
d 3 5 6 0

Again, I'd wrap this in a function if I had to do it more than once.

凯凯我们等你回来 2024-11-07 09:04:32

它是否必须是对角线上有零的镜像矩阵?

combo <- combn(letters[1:4], 2)
in.combo <- matrix(1:6, nrow = 1)
combo <- rbind(combo, in.combo)
out.combo <- matrix(rep(NA, 16), ncol = 4)
colnames(out.combo) <- letters[1:4]
rownames(out.combo) <- letters[1:4]

for(cols in 1:ncol(combo)) {
    vec1 <- combo[, cols]
    out.combo[vec1[1], vec1[2]] <- as.numeric(vec1[3])
}

> out.combo
   a  b  c  d
a NA  1  2  3
b NA NA  4  5
c NA NA NA  6
d NA NA NA NA

Does it have to be a mirror matrix with zeros over the diagonal?

combo <- combn(letters[1:4], 2)
in.combo <- matrix(1:6, nrow = 1)
combo <- rbind(combo, in.combo)
out.combo <- matrix(rep(NA, 16), ncol = 4)
colnames(out.combo) <- letters[1:4]
rownames(out.combo) <- letters[1:4]

for(cols in 1:ncol(combo)) {
    vec1 <- combo[, cols]
    out.combo[vec1[1], vec1[2]] <- as.numeric(vec1[3])
}

> out.combo
   a  b  c  d
a NA  1  2  3
b NA NA  4  5
c NA NA NA  6
d NA NA NA NA
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文