Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 2 columns dataframe to a 2 dimensional matrix in R

Hi I am trying to convert an R dataframe with 2 character columns to a matrix. Here is an example of what I am trying to do:

suppose I have this dataframe:

name  work 
one   test1
one   test2
two   test1
two   test3
three test3

And I would like to turn it into this matrix

name  test1  test2  test3 
one     1      1      0   
two     1      0      1   
three   0      0      1   

Any idea how I can accomplish this in R please?

like image 673
user1375640 Avatar asked Sep 21 '25 05:09

user1375640


1 Answers

DF <- read.table(text="name  work 
one   test1
one   test2
two   test1
two   test3
three test3", header=TRUE)

table(DF)

       work
name    test1 test2 test3
  one       1     1     0
  three     0     0     1
  two       1     0     1
like image 77
Roland Avatar answered Sep 22 '25 18:09

Roland