It's not trivial to learn the tricks, granted...but here's a start for the first...with it and some study of the examples, should get an idea -- it's late; I've got to turn in at this point, though..sorry :)
results in for the first problem statement...I chose "inf' as the indicator of no infection instead of NaN -- the decimals show up because I'd been doing financial work and had format bank in effect.
I slightly modified your data to a file with the header line "patient, testday, found" and made a csv-file of your plain text...I presume you have a file format of your own.
Modifications to the functional for the remainder should suffice I think...altho 3 and 4 need grouping by both patient and bacteria ID.
Added the identifier for which bacteria ID was the first. Having the standalone function means can clean up the return data there instead of afterwards--so there is some payback for the extra code. :)
NB: I got the return arguments from min in wrong order last night; the index is the optional second, not the first. This produces the amplified table:
Patient FirstInfection Infection
_______ ______________ _________
A 2.00 BAC2
B 55.00 BAC1
C 51.00 BAC9
D NaN NAN
A [1×3 categorical]
B [1×2 categorical]
C [1×2 categorical]
D [1×0 categorical]
Patient Bacterium FirstInfection Infection
_______ _________ ______________ _________
A BAC1 4.00 BAC1
A BAC2 2.00 BAC2
A BAC3 8.00 BAC3
B BAC1 55.00 BAC1
B BAC5 66.00 BAC5
C BAC2 77.00 BAC2
C BAC9 51.00 BAC9
D NAN NaN NAN
Unfortunately, the builtin table display function won't show the actual categorical variable values for each patient since they're not the same length of each array--and a table has to be regular in number of variable sfor each row/observation so can't create multiple variables without a lot of ugly NAN values scattered around.
The really cute part is the firstinfection function works for any chosen grouping so that don't have to do anything except use the other grouping variables. You could choose to not populate the tble with the second return or not use the second ID variable since they are the same...
Now, your mission, should you choose to accept it, is last item, #4... :)