我有一个如下数据框:
read.csv(text="num,placed,recovered
1,2013-02-22 12:14:00,2013-02-27 15:14:00
1,2013-03-03 17:32:00,2013-03-07 17:32:00
1,2013-04-24 10:13:00,2013-04-26 07:47:00
1,2013-04-15 14:51:00,2013-04-19 09:36:00
1,2013-04-11 11:56:00,2013-04-15 12:52:00
10,2013-02-22 07:30:00,2013-02-27 14:55:00
10,2013-03-03 17:20:00,2013-03-07 17:20:00
10,2013-04-15 15:22:00,2013-04-19 09:48:00
10,2013-02-17 10:38:00,2013-02-22 07:18:00
10,2013-04-11 10:09:00,2013-04-15 13:21:00
10,2013-04-24 10:07:00,2013-04-26 08:23:00
11,2013-02-22 14:23:00,2013-02-27 15:50:00
11,2013-04-11 12:51:00,2013-04-14 09:40:00
11,2013-04-15 14:45:00,2013-04-19 08:28:00
11,2013-04-19 10:13:00,2013-04-23 12:01:00
14,2013-03-01 13:45:00,2013-03-08 14:28:00
14,2013-02-22 13:22:00,2013-02-27 15:24:00
14,2013-04-04 15:36:00,2013-04-17 15:04:00",header=TRUE)
我想重新排列它,使每个输入num
出现一次,其所有placed
和recovered
值都在一行中。以下是示例行:
num placed1 recovered1 placed2 recovered2 placed3 recovered3 placed4 recovered4 placed5 recovered5
1 2013-02-22 12:14:00 2013-02-27 15:14:00 2013-03-03 17:32:00 2013-03-07 17:32:00 2013-04-24 10:13:00 2013-04-26 07:47:00 2013-04-15 14:51:00 2013-04-19 09:36:00 2013-04-11 11:56:00 2013-04-15 12:52:00
某些行将具有不同数量的放置和恢复值。NA
s出现在那些地方就好了。我试过使用重塑功能,但似乎无法得到我想要的东西。
我这样做是对我正在清理的数据集进行子集化的一个步骤。另一个数据集记录了随时间变化的测量值以及收集时间。获取数据的设备存储在num
列中。我想获取该数据帧的一个子集来仅获取放置该设备的时间间隔(每对数据placed
之间的时间recovered
)。因此,另一个数据框将如下所示:
num temp time
1 5 2013-02-22 12:13:50
1 6 2013-02-22 12:14:00
1 4 2013-02-22 12:14:10
1 9 2013-04-24 09:45:20
1 7 2013-04-24 11:45:50
10 23 2013-03-03 19:23:40
如果我能够成功对其进行子集化,结果将如下所示
num temp time
1 6 2013-02-22 12:14:00
1 4 2013-02-22 12:14:10
1 7 2013-04-24 11:45:50
10 23 2013-03-03 19:23:40