字符两列列表为数字矩阵

时间:2017-05-03 19:35:04

标签: r matrix dataframe

我有一个相对容易的问题,我有这个数据框有两个字符对象,称为a和b。像这样:

data<-data.frame(a=c("a","a","a","b","b","c","c","d"),
b=c("x","y","z","y","z","x","z","z"))

如何将此数据帧转换为如下所示的矩阵:

matrix<-matrix( c(1,1,1,0,1,1,1,0,1,0,0,1),nrow=4, ncol=3 )
rownames(matrix)<-c("a","b","c","d")
colnames(matrix)<-c("x","y","z")

我尝试过sapply和lapply的组合,并在线寻找答案但没有成功。

谢谢! 此致

0 个答案:

没有答案