0

I am trying to restructure my data using reshape from wide to long format but I keep getting an error. Below I wrote the code I've already tried and the error message I get.

current data structure

patientid  Adh_catv1    Adh_catv2    Adh_catv3    Adh_threeitemsv1   Adh_threeitemsv2  Adh_threeitemsv3
70FD       optimal      optimal      optimal      86                 90                100
70LJ       suboptimal   suboptimal   optimal      40                 50                70
70ML       optimal      suboptimal   suboptimal   89                 55                50        

desired structure

patientid  Visits    Adherence    Adherence_threeitem
70FD       visit1   optimal        86
70FD       visit2   optimal        90
70FD       visit3   optimal        100
70LJ       visit1   suboptimal     40
70LJ       visit2   suboptimal     50
70LJ       visit3   optimal        70
70ML       visit1   optimal        89
70ML       visit2   suboptimal     55
70ML       visit3   suboptimal     50

This is what I've tried so far


    reshape(df, direction = 'long',
        varying = c ('adh_catv1:Adh_threeitemsv3'),
        timevar = 'Visits',
        times = c ("visit1","visit2","visit3"),
        v.names = c ('adherence','adherence_threeitem),
        idvar = 'patientid')

Error in reshape(df, direction = "long", varying = c("adh_catv1:Adh_threeitemsv3"),  : 
  length of 'varying' must be the product of length of 'v.names' and length of 'times'

Please advise on what I am doing wrong in the code above or suggest an alternative easier option using other functions.

Thandi
  • 225
  • 1
  • 2
  • 9

1 Answers1

1

The most basic code here is:

reshape(df,-1, dir="long", sep="")

    patientid time   Adh_catv Adh_threeitemsv id
1.1      70FD    1    optimal              86  1
2.1      70LJ    1 suboptimal              40  2
3.1      70ML    1    optimal              89  3
1.2      70FD    2    optimal              90  1
2.2      70LJ    2 suboptimal              50  2
3.2      70ML    2 suboptimal              55  3
1.3      70FD    3    optimal             100  1
2.3      70LJ    3    optimal              70  2
3.3      70ML    3 suboptimal              50  3

You can add the other variables in order to have it as you want:

Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Mr. Onyambu, may I ask how we could reproduce the same output with `pivot_longer`? – Anoushiravan R Apr 21 '21 at 16:00
  • 1
    @AnoushiravanR the basic pivot_longer code will be `df%>%pivot_longer(-patientid, names_to = c(".value", "Visits"), names_pattern="(.*)(\\d+)")` – Onyambu Apr 21 '21 at 16:07
  • Thank you very much. I was struggling with the `names_sep` argument and did not realize I should have used `names_pattern` instead. I guess I have to improve my regex knowledge. – Anoushiravan R Apr 21 '21 at 16:10
  • 1
    @AnoushiravanR you can still use the `name_sep` instead of the `name_pattern`. ie just use `names_sep="(?<=\\D)(?=\\d)"` – Onyambu Apr 21 '21 at 16:12
  • May I ask what this regex actually represents? I'm so sorry my knowledge in this area leaves much to be desired. – Anoushiravan R Apr 21 '21 at 16:14
  • 1
    @AnoushiravanR just used a lookbehind, ie`(?<=\D)` means that the last matched value is not a digit. Then used a lookahead `(?=\d)` which means that the next value is a digit. If you look at the names of the wide dataframe, you the separator should be between the characters and the digits – Onyambu Apr 21 '21 at 16:18
  • Thank you very much Mr. Onyambu. You are one of my inspirations here to learn more about R every day. – Anoushiravan R Apr 21 '21 at 16:30
  • 1
    @AnoushiravanR gone through your work and its nice. you will know are in the long run – Onyambu Apr 21 '21 at 16:37
  • Thank you very much indeed. It's an honor for me to receive such kind words. It's almost a year and I am learning R as I need it for future studies (supply chain sector). I really enjoyed it so far and I hope I can use it in my future jobs greatly. Thank you again for checking my codes. I would also be pleased if I could be among your linkedin network. – Anoushiravan R Apr 21 '21 at 16:42