在R中创建每小时间隔

时间:2017-02-12 20:53:28

标签: r parsing time average

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

Count       Time 
    85     00:00:00
    86     00:15:00
    87     00:30:00
    88     00:45:00
    89     01:00:00
    90     01:15:00
    91     01:30:00

间隔是每15分钟,我想将时间汇总到每60分钟,然后取每小时的平均数。理想情况下,我的输出将在数据帧中。谢谢

编辑:这可能只需要平均每四行一次?如果有人知道该怎么做。

1 个答案:

答案 0 :(得分:1)

数据

# Create the PSSession
$Session = New-PSSession -ComputerName sccmserver

# Load the CM Module using Implicit Remoting
Import-Module -Name "C:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1" -PSSession $Session

#Set the CMSite as our current location to run the CM cmdlets
Invoke-Command -Session $Session {Set-Location -Path LOC:}

$SCCMdevices = Get-CMDevice -CollectionName "All Systems" | Where {$_.IsActive -like "True"} | Select-Object -Property Name, DeviceOS

$SCCMdevices = $SCCMdevices -replace 'Microsoft Windows NT Workstation 5.1', 'Windows XP' `
                        -replace 'Microsoft Windows NT Workstation 5.1 (Tablet Edition)', 'Windows XP' `
                        -replace 'Microsoft Windows NT Workstation 6.1', 'Windows 7' `
                        -replace 'Microsoft Windows NT Workstation 6.1 (Tablet Edition)', 'Windows 7' `
                        -replace 'Microsoft Windows NT Workstation 6.1 (Embedded)', 'Windows 7' `
                        -replace 'Microsoft Windows NT Workstation 6.2', 'Windows 8' `
                        -replace 'Microsoft Windows NT Workstation 6.2 (Tablet Edition)', 'Windows 8' `
                        -replace 'Microsoft Windows NT Workstation 6.3', 'Windows 8.1' `
                        -replace 'Microsoft Windows NT Workstation 6.3 (Tablet Edition)', 'Windows 8.1' `
                        -replace 'Microsoft Windows NT Workstation 10.0', 'Windows 10' `
                        -replace 'Microsoft Windows NT Workstation 10.0 (Tablet Edition)', 'Windows 10' `
                        -replace 'Microsoft Windows NT Server 5.2', 'Windows Server 2003' `
                        -replace 'Microsoft Windows NT Advanced Server 5.2', 'Windows Server 2003' `
                        -replace 'Microsoft Windows NT Server 6.0', 'Windows Server 2008' `
                        -replace 'Microsoft Windows NT Advanced Server 6.0', 'Windows Server 2008' `
                        -replace 'Microsoft Windows NT Server 6.1', 'Windows Server 2008 R2' `
                        -replace 'Microsoft Windows NT Advanced Server 6.1', 'Windows Server 2008 R2' `
                        -replace 'Microsoft Windows NT Server 6.2', 'Windows Server 2012' `
                        -replace 'Microsoft Windows NT Advanced Server 6.2', 'Windows Server 2012' `
                        -replace 'Microsoft Windows NT Server 6.3', 'Windows Server 2012 R2' `
                        -replace 'Microsoft Windows NT Advanced Server 6.3', 'Windows Server 2012 R2' `
                        -replace 'Microsoft Windows NT Server 10.0', 'Windows Server 2016' `
                        -replace 'Microsoft Windows NT Advanced Server 10.0', 'Windows Server 2016' `
                        -replace '6.1', 'Windows 7' `
                        -replace '10.0', 'Windows 10'

首先从时间中提取小时并将其用作分组变量以使用聚合

计算平均值
df = structure(list(Count = 85:91, Time = c("00:00:00", "00:15:00", 
"00:30:00", "00:45:00", "01:00:00", "01:15:00", "01:30:00"), 
    hour = c("00", "00", "00", "00", "01", "01", "01")), .Names = c("Count", 
"Time", "hour"), row.names = c(NA, -7L), class = "data.frame")