I am currently working on a Multistate Analysis dataset in "long" form (one row for each individual's observation; each individual is repeatedly measured up to 5 times).
The idea is that each individual can recurrently transition across the levels of the time-varying state variable s = 1, 2, 3, 4
. All the other variables that I have (here cohort
) are fixed within any given id
.
After some analyses, I need to reshape the dataset in "wide" form, according to the specific sequence of visited states. Here is an example of the initial long data:
dat <- read.table(text = "
id cohort s
1 1 2
1 1 2
1 1 1
1 1 4
2 3 1
2 3 1
2 3 3
3 2 1
3 2 2
3 2 3
3 2 3
3 2 4",
header=TRUE)
The final "wide" dataset should take into account the specific individual sequence of visited states, recorded into the newly created variables s1
, s2
, s3
, s4
, s5
, where s1
is the first state visited by the individual and so on.
According to the above example, the wide dataset looks like:
id cohort s1 s2 s3 s4 s5
1 1 2 2 1 4 0
2 3 1 1 3 0 0
3 2 1 2 3 3 4
I tried to use reshape()
, and also to focus on transposing s
, but without the intended result. Actually, my knowledge of the R functions is quite limited.. Can you give any suggestion? Thanks.
EDIT: obtaining a different kind of wide dataset
Thank you all for your help, I have a related question if I can. Especially when each individual is observed for a long time and there are few transitions across states, it is very useful to reshape the initial sample dat
in this alternative way:
id cohort s1 s2 s3 s4 s5 dur1 dur2 dur3 dur4 dur5
1 1 2 1 4 0 0 2 1 1 0 0
2 3 1 3 0 0 0 2 1 0 0 0
3 2 1 2 3 4 0 1 1 2 1 0
In practice now s1
-s5
are the distinct visited states, and dur1
-dur5
the time spent in each respective distinct visited state.
Can you please give a hand for reaching this data structure? I believe it is necessary to create all the dur
- and s
- variables in an intermediate sample before using reshape()
. Otherwise maybe it is possible to directly adopt -reshape2-
?
ok...
This seem right? I admit the first
ddply
is hardly elegant.Try this:
which gives this:
If you did not mind using just 1, 2, ..., 5 as column names then you could shorten the
ave
line to just:Regarding the second question that was added later try this:
which gives:
Gives:
then using the following line gives your names:
If you use
sep=''
in thewide
statement you do not have to rename the variables:I suspect there are ways to avoid creating the
period
variable and avoid replacingNA
directly in thewide
statement, but I have not figured those out yet.