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

How can I transpose my table by selected column values in R?

I’m working in R.
how can I transpose my table creating new columns by client. The rows would be by idMarket and Section, and the other columns would give the Score of each client in those Markets and Section.

idMarket  idSection idClient  Score  %

2          99         23       100    1
2          99         56       25     0,2
3          67         23       56     0,5
3          67         56       50     0,3

Expected table:


idMarket  idSection  Client23  %23   Client56   %56

2          99         100      1        25      0,2
3          67          56      0,5      50      0,3


Thanks!!

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 :

pivot_wider(df, c(idMarket, idSection), names_from = idClient, values_from = c(Score, `%`))
# A tibble: 2 x 6
  idMarket idSection Score_23 Score_56 `%_23` `%_56`
     <int>     <int>    <int>    <int>  <dbl>  <dbl>
1        2        99      100       25    1      0.2
2        3        67       56       50    0.5    0.3


reshape(df, dir='wide', timevar = 'idClient', idvar = c('idMarket', 'idSection'), sep='_')
  idMarket idSection Score_23 %_23 Score_56 %_56
1        2        99      100  1.0       25  0.2
3        3        67       56  0.5       50  0.3
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