0

I have a large dataset with 30k rows like below:

patno    Inentorname 

1001       A
1001       B
1002       A
1002       B
1003       C
1004       D
1004       E

What I want to get is like:

   A  B  C  D  E 
A  0  2  0  0  0
B  2  0  0  0  0
C  0  0  0  0  0
D  0  0  0  0  1
E  0  0  0  1  0

Is there any easy way to get this in R or python?

mpx
  • 3,081
  • 2
  • 26
  • 56
Feixiang Sun
  • 117
  • 6
  • @balandongiv [What have you tried? is blocked for a reason](https://meta.stackexchange.com/a/172760) – Scratte Jul 09 '21 at 08:45

1 Answers1

1

Assuming DF shown reproducibly in the Note at the end

tab <- crossprod(table(DF))
diag(tab) <- 0
tab

giving:

           Inentorname
Inentorname A B C D E
          A 0 2 0 0 0
          B 2 0 0 0 0
          C 0 0 0 0 0
          D 0 0 0 0 1
          E 0 0 0 1 0

Note

Lines <- "
patno    Inentorname 
1001       A
1001       B
1002       A
1002       B
1003       C
1004       D
1004       E"
DF <- read.table(text = Lines, header = TRUE)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341