我有以下数据框(DF1):
Date Value
29/12/2014 8:00 24.940
29/12/2014 9:00 24.960
29/12/2014 11:00 25.020
我想为DF1 $ DIFF添加一个新列,其中包含每行的日期(包括小时)与其上述日期之间的值差异。这样所需的结果将是:
Date Value Diff
29/12/2014 8:00 24.940
29/12/2014 9:00 24.960 1
29/12/2014 11:00 25.020 2
我尝试使用as.date函数,但是,我只得到日期的差异:
> as.Date("2009-10-01 10:00")- as.Date("2009-10-01 9:00")
Time difference of 0 days
答案 0 :(得分:2)
最初,如果您想查看小时数差异,则无法使用as.Date
,因为这会丢弃小时信息。而是使用as.POSIXct
。然后,您可以计算差异:
as.POSIXct(strptime("2009-10-01 10:00:00", "%Y-%m-%d %H:%M:%S")) -
as.POSIXct(strptime("2009-10-01 09:00:00", "%Y-%m-%d %H:%M:%S"))
给你Time difference of 1 hours
。
答案 1 :(得分:2)
尝试使用包<?php
require('fpdf.php');
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$colNames = array();
foreach($db->query("SELECT * FROM mytest") as $row2) {
echo $row2['id'].' '.$row2['name'];
}
$result = $db->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='mytest'");
foreach($result as $row) {
$colNames[] = $row["COLUMN_NAME"];
}
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetDisplayMode(real,'default');
$pdf->SetFont('Arial','B',12);
$pdf->SetTextColor(50,60,100);
$pdf->SetXY(50,20);
$pdf->SetDrawColor(50,60,100);
$pdf->Cell(100,10,'Tabledata MyTest',1,0,'C',0);
$pdf->Ln();
$pdf->Ln();
foreach($colNames as $colName) {
$pdf->Cell(90,12,$colName,1);
}
$pdf->Ln();
foreach($db->query("SELECT * FROM mytest") as $row) {
$pdf->Ln();
foreach($colNames as $colName) {
$pdf->Cell(90,12,$row[$colName],1);
}
}
ob_end_clean();
$pdf->Output();
?>
。您可以使用lubridate
lubridate
或
library(lubridate)
difftime(ymd_hm("2009-10-01 10:00"), ymd_hm("2009-10-01 9:00"))
#Time difference of 1 hours
另一个例子
ymd_hm("2009-10-01 10:00") - ymd_hm("2009-10-01 9:00")
#Time difference of 1 hours
或
difftime(dmy_hm("29/12/2014 11:00"), dmy_hm("29/12/2014 9:00"), units="hours")
#Time difference of 2 hours