【问题标题】:Using Time Diary Data with TraMineR使用 TraMineR 使用时间日记数据
【发布时间】:2012-08-03 18:43:24
【问题描述】:

我正在尝试使用 R 中的 TraMineR 使用时间日记数据(美国时间使用调查)进行序列分析。我将数据作为 SPELL 数据(id、开始时间、停止时间、事件)但我收到以下错误尝试将其转换为 STS 或 SPS 数据时:

as.matrix.data.frame(subset(data, , 2)) 中的错误:dims [product 0] 与对象 [9] 的长度不匹配

我相信这与我如何将时间(作为字符)转换为日期/时间类型有关。我相信 TraMineR 需要 POSIXlt 格式?

这是我的原始数据的 sn-p(trcode 是事件)

头部(atus.act.short)

     tucaseid tustarttim tustoptime trcode
1 2.00701e+13   04:00:00   08:00:00  10101
2 2.00701e+13   08:00:00   08:20:00 110101
3 2.00701e+13   08:20:00   08:50:00  10201
4 2.00701e+13   08:50:00   09:30:00  20102
5 2.00701e+13   09:30:00   09:40:00 180201
6 2.00701e+13   09:40:00   11:40:00  20102

我使用 strptime 将字符串转换为 POSIXlt:

atus.act.short$starttime.new <- strptime(atus.act.short$tustarttim, format="%X")
atus.act.short$stoptime.new  <- strptime(atus.act.short$tustoptime, format="%X")

我还将 ID 减少到只有两位数

  atus.act.short$id <- atus.act.short$tucaseid-20070101070000

我最终得到一个新的数据框,如下所示:

   id       starttime.new        stoptime.new trcode
1  44 2012-08-03 04:00:00 2012-08-03 08:00:00  10101
2  44 2012-08-03 08:00:00 2012-08-03 08:20:00 110101
3  44 2012-08-03 08:20:00 2012-08-03 08:50:00  10201
4  44 2012-08-03 08:50:00 2012-08-03 09:30:00  20102
5  44 2012-08-03 09:30:00 2012-08-03 09:40:00 180201
6  44 2012-08-03 09:40:00 2012-08-03 11:40:00  20102
7  44 2012-08-03 11:40:00 2012-08-03 11:50:00 180201
8  44 2012-08-03 11:50:00 2012-08-03 12:05:00  20102
9  44 2012-08-03 12:05:00 2012-08-03 13:05:00 120303
10 44 2012-08-03 13:05:00 2012-08-03 13:20:00 180704
11 44 2012-08-03 13:20:00 2012-08-03 15:20:00  70104
12 44 2012-08-03 15:20:00 2012-08-03 15:35:00 180704
13 44 2012-08-03 15:35:00 2012-08-03 17:00:00 120303
14 44 2012-08-03 17:00:00 2012-08-03 17:20:00 180701
15 44 2012-08-03 17:20:00 2012-08-03 17:25:00 180701
16 44 2012-08-03 17:25:00 2012-08-03 17:55:00  70101
17 44 2012-08-03 17:55:00 2012-08-03 18:00:00 181203
18 44 2012-08-03 18:00:00 2012-08-03 19:00:00 120303
19 44 2012-08-03 19:00:00 2012-08-03 19:30:00 110101
20 44 2012-08-03 19:30:00 2012-08-03 21:30:00 120303
21 44 2012-08-03 21:30:00 2012-08-03 23:00:00  10101
22 44 2012-08-03 23:00:00 2012-08-03 23:03:00  10201
26 48 2012-08-03 06:45:00 2012-08-03 08:15:00  10201
27 48 2012-08-03 08:15:00 2012-08-03 08:45:00 180209
28 48 2012-08-03 08:45:00 2012-08-03 09:00:00  20902
29 48 2012-08-03 09:00:00 2012-08-03 11:00:00  50101
30 48 2012-08-03 11:00:00 2012-08-03 11:45:00 120312

然后我尝试[使用 library(TraMineR)] 创建一个序列对象

atus.seq <- seqdef(atus.act.short, informat = "SPELL", id="id")

我收到以下错误:

as.matrix.data.frame(subset(data, , 2)) 中的错误:dims [product 0] 与对象 [9] 的长度不匹配

想法?

【问题讨论】:

  • 我设法通过将时间转换为分钟(在 stackoverflow 上的另一个问题之后)、使用 seqformat 将状态代码设置为字符(as.character)并将其分配给时间轴。新代码为:atus.seq2 &lt;- seqformat(atus.act.short2, id="id", from="SPELL", to="STS", begin = "startmin", end = "stopmin", status="trcode", process = "FALSE")
  • 你能把这个作为答案发布并接受它作为正确答案吗?

标签: r traminer


【解决方案1】:

我设法通过将时间转换为分钟(在 stackoverflow 上的另一个问题之后)、使用 seqformat 将状态代码设置为字符(as.character)并将其分配给时间轴来解决此问题。新代码如下:

atus.seq2 &lt;- seqformat(atus.act.short2, id="id", from="SPELL", to="STS", begin = "startmin", end = "stopmin", status="trcode", process = "FALSE")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多