R规范化数据集

时间:2016-04-17 10:53:43

标签: r normalize melt

我有一个看起来像这样的数据集

> dput(events.seq)
structure(list(vid = structure(1L, .Label = "2a38ebc2-dd97-43c8-9726-59c247854df5", class = "factor"), 
    deltas = structure(1L, .Label = "38479,38488,38492,38775,45595,45602,45606,45987,50280,50285,50288,50646,54995,55001,55005,55317,59528,59533,59537,59921,63392,63403,63408,63822,66706,66710,66716,67002,73750,73755,73759,74158,77999,78003,78006,78076,81360,81367,81371,82381,93365,93370,93374,93872,154875,154878,154880,154880,155866,155870", class = "factor"), 
    events = structure(1L, .Label = "mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown,mouseup,click,mousemove,mousedown", class = "factor")), .Names = c("vid", 
"deltas", "events"), class = "data.frame", row.names = c(NA, 
-1L))

我需要将其标准化为此结构:

> dput(test)
structure(list(vid = structure(c(1L, 1L, 1L), .Label = "2a38ebc2-dd97-43c8-9726-59c247854df5\n+ ", class = "factor"), 
    delta = c(38479, 38488, 38492), c..mousemove....mousedown....mousup.. = structure(c(2L, 
    1L, 3L), .Label = c("mousedown", "mousemove", "mousup"), class = "factor")), .Names = c("vid", 
"delta", "c..mousemove....mousedown....mousup.."), row.names = c(NA, 
-3L), class = "data.frame")

任何帮助表示赞赏。 我确实尝试使用strplit,问题是我想在第二和第三列上同时拆分两次(它们的长度始终同步)

1 个答案:

答案 0 :(得分:0)

试试这个:

z <- with(x, data.frame(
  deltas = strsplit(as.character(deltas), split = ",")[[1]],
  events = strsplit(as.character(events), ",")[[1]]
))
head(z)

结果:

  deltas    events
1  38479 mousemove
2  38488 mousedown
3  38492   mouseup
4  38775     click
5  45595 mousemove
6  45602 mousedown