在R中,如何使用combn检索完整的矩阵?
我的问题,删除特定目的,看起来像这样: 如何转换这样的组合: 首先使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种可行的方法:
给出:
如果我必须经常这样做,我会将这些函数调用包装到一个函数中。
另一种选择是使用
as.matrix.dist()
通过手动设置"dist"
对象来为我们进行转换。使用之前的一些对象:这给出了:
再次,如果我必须多次执行此操作,我会将其包装在一个函数中。
Here is one way that works:
Which gives:
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:Which gives:
Again, I'd wrap this in a function if I had to do it more than once.
它是否必须是对角线上有零的镜像矩阵?
Does it have to be a mirror matrix with zeros over the diagonal?