1

I am trying to convert data of this form to STS format in order to perform sequence analysis:

|Person ID |Spell |Start Month |End Month |Status (Economic Activity) |
| -------- |----- |------------|----------|---------------------------|            
|1|1|300|320|4|
|1|2|320|360|4|
|2|1|330|360|4|
|3|1|270|360|7|
|4|1|280|312|4|
|4|2|312|325|4|
|4|3|325|360|6|

Does anyone know how I can deal with the issue of multiple spells per person and somehow combine each spell for a given individual?

Gilbert
  • 3,570
  • 18
  • 28
James
  • 21
  • 2

1 Answers1

1

You should have a look at TraMiner's excellent documentation. Particularly, the user guide is very helpful. There you would find a section on the seqformat function, which is exactly what you are looking for

library(TraMineR)

## Create spell data
data <-
  as.data.frame(
    matrix(
      c(1, 1, 300, 320, 4,   
        1, 2, 320, 360, 4, 
        2, 1, 330, 360, 4, 
        3, 1, 270, 360, 7, 
        4, 1, 280, 312, 4, 
        4, 2, 312, 325, 4, 
        4, 3, 325, 360, 6),
      ncol = 5, byrow = T)
  )

names(data) <- c("id", "spell", "start", "end", "status")


## Converting from SPELL to STS format with TraMineR::seqformat
data.sts <- 
  seqformat(data, from = "SPELL", to = "STS",
            id = "id", begin = "start", end = "end", status = "status",
                      process = FALSE)
maraab
  • 425
  • 3
  • 10
  • Thank you for your help Maraab. I am unsure how to do the above matrix with a large dataset: I have 435,635 rows so I cannot type it by hand. Is there a way to program the matrix you have written out? – James Jul 28 '22 at 07:34
  • I suppose your data are stored in a an excel file or as a csv file. There exist multiple R packages that help you to import all kind of data formats: - https://readr.tidyverse.org/ - https://readxl.tidyverse.org/ - https://haven.tidyverse.org/ - https://github.com/leeper/rio – maraab Jul 28 '22 at 07:53