将分类属性向量转换为相似度矩阵 [英] Transform categorical attribute vector into similarity matrix

查看:207
本文介绍了将分类属性向量转换为相似度矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用R将分类属性向量转换为相同属性矩阵".

I need to transfrom a categorical attribute vector into a "same attribute matrix" using R.

例如,我有一个向量,该向量报告N人的性别(男性= 1,女性= 0).我需要将此向量转换为名为A的NxN矩阵(行和列上都有人的名字),如果两个人(i和j)的性别相同,则每个单元格Aij的值为1,否则为0.

For example I have a vector which reports gender of N people (male = 1, female = 0). I need to convert this vector into a NxN matrix named A (with people names on rows and columns), where each cell Aij has the value of 1 if two persons (i and j) have the same gender and 0 otherwise.

下面是一个示例,其中有3个人(第一位男性,第二位女性,第三位男性)产生此向量:

Here is an example with 3 persons, first male, second female, third male, which produce this vector:

c(1, 0, 1) 

我想将其转换为这个矩阵:

I want to transform it into this matrix:

A = matrix( c(1, 0, 1, 0, 1, 0, 1, 0, 1), nrow=3, ncol=3, byrow = TRUE) 

推荐答案

像lmo所说,不可能知道数据集的结构,因此下面仅是一个示例,供您了解如何完成.
首先,整理一些数据.

Like lmo said in acomment it's impossible to know the structure of your dataset so what follows is just an example for you to see how it could be done.
First, make up some data.

set.seed(3488)    # make the results reproducible
x <- LETTERS[1:5]
y <- sample(0:1, 5, TRUE)
df <- data.frame(x, y)

现在根据需要将其制成表格

Now tabulate it according to your needs

A <- outer(df$y, df$y, function(a, b) as.integer(a == b))
dimnames(A) <- list(df$x, df$x)
A
#  A B C D E
#A 1 1 1 0 0
#B 1 1 1 0 0
#C 1 1 1 0 0
#D 0 0 0 1 1
#E 0 0 0 1 1

这篇关于将分类属性向量转换为相似度矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆