1

tibble

enter image description here

How can I convert above tibble into following output matrix?

(how I can split PT then Pessary put the value 172 in to the position (2,1) and also for the rest value?)

(Note Pessary then Pessary = 809 can be added to Pessary = 6238, In ouput it should be 6238+809 not 6238, same for all others)

output Image

enter image description here

mrbonewithgale
  • 100
  • 1
  • 7
  • Post your data as text, make it reproducible `dput(mydata)`. – zx8754 Jun 21 '21 at 06:51
  • Images are not the right way to share data/code. Add them in a reproducible format which is easier to copy. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Try the solutions from this post https://stackoverflow.com/questions/11322801/transpose-reshape-dataframe-without-timevar-from-long-to-wide-format – Ronak Shah Jun 21 '21 at 06:52

1 Answers1

0

A data.table option

xtabs(
    n ~ .,
    setDT(df)[
        ,
        c(tstrsplit(subsequent_treatments, " then "), .(n = n))
    ][
        ,
        V2 := fcoalesce(V1, V2)
    ]
)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81