Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: how to convert a data frame to an assymetric matrix with an empty corner

I have the following data frame:

table <- data.frame(pop_1 = c("AL","AL","AL","AL","AL","AL","AL","ALT","ALT","ALT","ALT","ALT","ALT","BU","BU","BU","BU","BU","IRK","IRK","IRK","IRK","KK","KK","KK","KYA","KYA","TU"),
                    pop_2 = c("ALT","BU","IRK","KK","KYA","TU","ZAB","BU","IRK","KK","KYA","TU","ZAB","IRK","KK","KYA","TU","ZAB","KK","KYA","TU","ZAB","KYA","TU","ZAB","TU","ZAB","ZAB"),
                    value = c(0.43447,0.15267,0.25912,0.10435,0.19238,0.19186,0.18155,0.34969,0.07506,0.29206,0.13597,0.46354,0.17870,0.18658,0.02297,0.08851,0.18950,0.05176,0.12086,0.02690,0.29669,0.05551,0.04910,0.15779,0.03276,0.23422,0.00568,0.22181))

How to convert it to an asymmetric matrix with empty (or NA, etc.) cells like this:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Minor change to your data frame, extra "AL", "AL", "NA" combination at the start. You’ll want to do the same for an extra "ZAB" at the end:

df<- data.frame(pop_1 = c("AL","AL","AL","AL","AL","AL","AL","AL","ALT","ALT","ALT","ALT","ALT","ALT","BU","BU","BU","BU","BU","IRK","IRK","IRK","IRK","KK","KK","KK","KYA","KYA","TU"),
              pop_2 = c("AL","ALT","BU","IRK","KK","KYA","TU","ZAB","BU","IRK","KK","KYA","TU","ZAB","IRK","KK","KYA","TU","ZAB","KK","KYA","TU","ZAB","KYA","TU","ZAB","TU","ZAB","ZAB"),
              value = c(NA,0.43447,0.15267,0.25912,0.10435,0.19238,0.19186,0.18155,0.34969,0.07506,0.29206,0.13597,0.46354,0.17870,0.18658,0.02297,0.08851,0.18950,0.05176,0.12086,0.02690,0.29669,0.05551,0.04910,0.15779,0.03276,0.23422,0.00568,0.22181))

library(tidyverse)
pivot_wider(df, names_from=pop_1, values_from=value)

 pop_2     AL     ALT      BU     IRK      KK      KYA     TU
  <chr>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>    <dbl>  <dbl>
1 AL    NA     NA      NA      NA      NA      NA       NA    
2 ALT    0.434 NA      NA      NA      NA      NA       NA    
3 BU     0.153  0.350  NA      NA      NA      NA       NA    
4 IRK    0.259  0.0751  0.187  NA      NA      NA       NA    
5 KK     0.104  0.292   0.0230  0.121  NA      NA       NA    
6 KYA    0.192  0.136   0.0885  0.0269  0.0491 NA       NA    
7 TU     0.192  0.464   0.190   0.297   0.158   0.234   NA    
8 ZAB    0.182  0.179   0.0518  0.0555  0.0328  0.00568  0.222
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading