[Traminer-users] Format of sequences
Anton Perdoncin
anton.perdoncin at gmail.com
Thu Jun 11 12:22:17 CEST 2015
Hello,
Thanks a lot Thomas, Hadrien and Gilbert.
The HSPELL_TO_STS function is very usefull indeed, but it supposes that
begin and end variables be numeric, which is not my case since I have
dates. And converting dates into numbers originating from the most
ancient date of the first episode seems to me much quicker with SPELL
format.
So here is one way of converting my HSPELL data into SPELL. Very close
to Thomas' suggestion (might not be the most elegant script... but it
does the job):
d <- read.csv2("~ ... example.csv")
f <- data.frame(ident=NULL,deb=NULL,fin=NULL,etat=NULL,motif=NULL)
i <- 1
for(i in 1:20){
e <- d[,names(d) %in%
c("ident",paste0(c("deb","fin","etat","motif"),as.character(i)))]
names(e) <- c("ident","deb","fin","etat","motif")
f <- rbind(f,e)
}
# ordering by identifiers
f <- f[order(f$ident),]
# deleting empty rows
f <- subset(f, deb!="")
# counting the number of episodes by individual
dim <- nrow(f)
f <- cbind(f,rep(1,dim))
colnames(f)[6]<-"nbepis"
for (i in 1:(dim-1))
{
if (f[i+1, 1] == f[i, 1])
{
f[i+1, 6] <- f[i, 6]+1
}
}
# checking that maximum number of episodes is equal to 20
max(f$nbepis)
For those who would like to see the actual result: see the example
dataset attached.
Best,
Anton
Le 11/06/2015 08:57, Gilbert Ritschard a écrit :
>
> You could also consider the HSPELL_to_STS function provided by the
> TraMineRextras package.
>
> *From:*traminer-users-bounces at lists.r-forge.r-project.org
> [mailto:traminer-users-bounces at lists.r-forge.r-project.org] *On Behalf
> Of *Hadrien Commenges
> *Sent:* Thursday, June 11, 2015 08:45
> *To:* Users questions
> *Subject:* Re: [Traminer-users] Format of sequences
>
> You could also split your table by set of columns (columns 1:4, then
> col c(1, 5:7), etc.) and then rbind() all the tables.
>
> Another option would be the melt() function in the reshape2 package.
>
> ------------------------------------------------------------------------
>
> *De: *"thomas collas" <thomas.collas at gmail.com
> <mailto:thomas.collas at gmail.com>>
> *À: *"Users questions" <traminer-users at lists.r-forge.r-project.org
> <mailto:traminer-users at lists.r-forge.r-project.org>>
> *Envoyé: *Mercredi 10 Juin 2015 21:03:05
> *Objet: *Re: [Traminer-users] Format of sequences
>
> Addendum : Do not forget to keep the ID column with the three other
> columns at each iteration.
>
> 2015-06-10 21:01 GMT+02:00 thomas collas <thomas.collas at gmail.com
> <mailto:thomas.collas at gmail.com>>:
>
> Hello Anton,
>
> An easy solution is to build a very short loop (I know R is not
> made for loops but it's only 18 iterations) separating each group
> of three columns, turning the headings into common ones
> (begin/end/state) and pasting each one below the other.
>
> I hope that helps,
>
> thomas collas
>
> 2015-06-10 18:51 GMT+02:00 Anton Perdoncin
> <anton.perdoncin at gmail.com <mailto:anton.perdoncin at gmail.com>>:
>
> Hi,
>
> I have sequences in the following format :
>
> ID BEGIN1 END1 STATE1 BEGIN2 END2
> STATE2 etc... until 18
> 1 01/01/1950 01/01/1960 X 02/01/1960 30/01/1960
> Y ...
> 2 01/01/1950 01/01/1960 X 02/01/1960 30/01/1960
> Y ...
>
> One line = one individual. Successive episodes = successive
> columns.
>
> I know that I need to convert dates into numbers: no problem
> with that.
>
> However, does anyone have any idea on how I could convert such
> a df into
> an STS or SPELL format ?
>
> Thanks!
>
> Best regards,
>
> Anton Perdoncin
> _______________________________________________
> Traminer-users mailing list
> Traminer-users at lists.r-forge.r-project.org
> <mailto:Traminer-users at lists.r-forge.r-project.org>
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users
>
>
> _______________________________________________
> Traminer-users mailing list
> Traminer-users at lists.r-forge.r-project.org
> <mailto:Traminer-users at lists.r-forge.r-project.org>
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users
>
>
>
> _______________________________________________
> Traminer-users mailing list
> Traminer-users at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/traminer-users/attachments/20150611/da64c47b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: example.csv
Type: text/csv
Size: 2250 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/traminer-users/attachments/20150611/da64c47b/attachment.csv>
More information about the Traminer-users
mailing list