1

这是一项非常具体的任务,但我相信从中学习可能足够普遍,可以使用。我有一个设计奇特的时间戳数据集,我需要对其进行重塑,并且需要使用基本安装附带的包进行操作。我认为看比解释容易。

带有时间戳的命名向量列表:

x <- structure(list(A = c("2.40", ":", "3.00", "5.01", "6.62", ":", 
    "7.00", "9.00"), B = c("2.40", "5.01", "6.62", ":", "7.00", "9.00"
    ), C = c("2.40", ":", "3.00", "5.01", "6.62", ":", "7.00", "9.00"
    )), .Names = c("A", "B", "C"))

列表的样子:

> x
[[1]]
[1] "2.40" ":"    "3.00" "5.01" "6.62" ":"    "7.00" "9.00"

[[2]]
[1] "2.40" "5.01" "6.62" ":"    "7.00" "9.00"

[[3]]
[1] "2.40" ":"    "3.00" "5.01" "6.62" ":"    "7.00" "9.00"

我想要什么:

$A
     start      end  
1   "2.40"   "3.00"   
2   "5.01"   "5.01"    
3   "6.62"   "7.00"   
4   "9.00"   "9.00"  

$B
     start      end 
1   "2.40"   "2.40"   
2   "5.01"   "5.01"  
3   "6.62"   "7:00"   
4   "9.00"   "9.00"   

$C
     start      end 
1   "2.40"   "3.00"
2   "5.01"   "5.01" 
3   "6.62"   "7.00"
4   "9.00"   "9.00"

如果有冒号 (:),左边的元素是开始值,右边的元素是结束值。如果一个元素没有接触冒号,则它需要重复,并且既是开始值又是结束值。

注意:如果它是数据框与矩阵,则结果不会有引号

4

1 回答 1

11

这是你想要的吗?

> lapply(x, function(v)
+   data.frame(start = v[-c(f <- which(v==":"), f + 1)], 
+              end = v[-c(f, f-1)]))
$A
  start  end
1  2.40 3.00
2  5.01 5.01
3  6.62 7.00
4  9.00 9.00

$B
  start  end
1  2.40 2.40
2  5.01 5.01
3  6.62 7.00
4  9.00 9.00

$C
  start  end
1  2.40 3.00
2  5.01 5.01
3  6.62 7.00
4  9.00 9.00
于 2012-10-18T07:35:37.143 回答