[R] How to split a Spell Data Frame into long format (irregular episode length)
arun
smartpink111 at yahoo.com
Sat Mar 22 15:32:30 CET 2014
Hi,
You could try:
res <- data.frame(dat[rep(1:nrow(dat),with(dat,end-begin+1)),1:2], month=unlist(apply(dat[,3:4],1,function(x) seq(x[1],x[2]))))
row.names(res) <- 1:nrow(res)
A.K.
On Saturday, March 22, 2014 6:29 AM, PabloNeruda <s_ysinm5 at uni-bremen.de> wrote:
Dear R-Users and developers,
I am used to work with singular data but now I have to get my data of an
spell-data-frame conducted in a big German longitudinal survey.
The data structure is:
persnr spelltyp begin end
xf105 10 1 5
xf105 1 6 15
xf106 4 7 12
xf106 7 13 15
xf106 1 16 20
The variable "spelltyp" means a type of employment, 1 meaning
full-time-employment, 4 meaning small part-time employment and so on. The
begin and end are months, beginning with the first month of survey data
collection as 1 (goes up to month 340).
My goal is to transform it into a long-format file such as
persnr spelltyp month
xf105 10 1
xf105 10 2
xf105 10 3
xf105 10 4
xf105 10 5
xf105 1 6
xf105 1 7
xf105 1 8
xf105 1 9
xf105 1 10
xf105 1 11
xf105 1 12
xf105 1 13
xf105 1 14
xf105 1 15
xf106 4 7
xf106 4 8
xf106 4 9
xf106 4 10
xf106 4 11
xf106 4 12
xf106 7 13
xf106 7 14
xf106 7 15
xf106 1 16
xf106 1 17
xf106 1 18
xf106 1 19
xf106 1 20
So what I want to do with R is to write a new row for every month and create
a new variable where the number of the month is written in (one could keep
the variables begin and end).
Later on I want to reshape it into I wide-format to perform a sequence
analysis.
I am grateful for any advise - Thanks!
--
View this message in context: http://r.789695.n4.nabble.com/How-to-split-a-Spell-Data-Frame-into-long-format-irregular-episode-length-tp4687335.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list