小编mCo*_*rey的帖子

使用TraMineR的时间日记数据

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

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

我认为这与我如何将时间(作为角色)转换为日期/时间类型有关.我相信TraMineR需要一个POSIXlt格式?

这是我原始数据的片段(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
Run Code Online (Sandbox Code Playgroud)

我使用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")
Run Code Online (Sandbox Code Playgroud)

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

  atus.act.short$id <- atus.act.short$tucaseid-20070101070000
Run Code Online (Sandbox Code Playgroud)

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

   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 …
Run Code Online (Sandbox Code Playgroud)

r traminer

5
推荐指数
1
解决办法
499
查看次数

在R中的数据框中为某些情况添加值

我有一个数据框,其中包含一个名为store的数字列,其中包含一些负值.我想添加1440到负面,但我遇到了麻烦.我的数据如下:

   score
  1  816
  2 -200
  3  976
  4 -376
  5    1
  6  121
  7 -331
Run Code Online (Sandbox Code Playgroud)

我可以使用替换值temp[temp$score< 0] <-8888.

但是,当我尝试使用:为变量添加值时temp[temp$score < 0] <- temp$score + 1440,我收到一条警告:

Warning message: In temp$score[temp$score < 0] <- temp$score + 1440 
:number of items to replace is not a multiple of replacement length
Run Code Online (Sandbox Code Playgroud)

然后我得到了一些奇怪的值:

  score
1   816
2  2256
3   976
4  1240
5     1
6   121
7  2416
Run Code Online (Sandbox Code Playgroud)

我是在调用函数错误还是我选择了错误的案例?

r

0
推荐指数
1
解决办法
662
查看次数

标签 统计

r ×2

traminer ×1