R:数据框中每小时一个值

时间:2017-06-21 11:43:15

标签: r dataframe gps reducing spatial-data

我在几天内从5个GPS设备('节点')获得了大约5600行坐标的GPS数据集,我想将GPS点数减少到每个点一个点小时。因为每小时的点数波动,所以不可能进行简单的for循环。 该表的简化结构如下:

ID  node  easting  northing  year  month  day  hour  minute  time

专栏'时间'是班级"POSIXlt" "POSIXt"。 尝试我的第一种方法,多个嵌套的for循环,我了解了Second circle of Inferno

有人有任何想法,如何将多行(每小时)减少到一个(每小时),由R中的每个设备分隔。

3 个答案:

答案 0 :(得分:2)

假设年,月,日和时间列包含与时间列相关的信息,解决方案可能如下:

# Generate data

md <- data.frame(
  node = rep(1:5, each = 2)
  , easting = sample(1:10, size = 20, replace = TRUE)
  , northing = sample(1:10, size = 20, replace = TRUE)
  , year =  2017
  , month = "June "
  , day = 6
  , hour = rep(1:2, each = 2, times = 5)
  , minute = NA
  , time = NA
)

# Solution

library(dplyr)

md %>% 
  group_by(node, year, month, day, hour) %>%
  summarize(
    easting = mean(easting),
    northing = mean(northing)
  )

答案 1 :(得分:1)

您可以创建一个新列&#34; Unix_hour&#34;:UNIX时间戳除以3600。

因此,每小时都会有一个唯一的ID。

要执行此操作,您应该使用as.numeric将POSIXct日期转换为Unix时间戳(以秒为单位):

as.numeric(POSIXct_variable) / 3600

它将返回时间戳。

然后,您将在这个新专栏上分组#34; Unix_hour&#34;:

aggregate(. ~ Unix_hour, df, mean)

(更改聚合函数&#34;表示&#34;如果您以其他方式聚合其他变量)

答案 2 :(得分:0)

您可以将日期时间的多列转换为一列,例如:

import psutil

power_notification = 0

def check_power_status():
    global power_notification
    battery = psutil.sensors_battery()
    plugged_in = battery.power_plugged
    percent = str(battery.percent)

    if not plugged_in:
        if power_notification == 0:
            plugged_in = "We are on battery power."
            print(plugged_in + "\n" + "You have " + percent + "% of your battery remaining.")
            power_notification = 1
    #AC plugged_in 
    else:
        power_notification = 0

while True:
    check_power_status()

使用DateTimeUTCmin5 <- ISOdate(year = tmp$Year, month = tmp$Month, day = tmp$Day, hour = tmp$Hour, min = tmp$Min, sec = tmp$Sec, tz = "America/New_York") 中的floor_date添加小时数

lubridate

然后决定您要如何从该小时中提取数据,首先是最大吗?

df$HourFloor = floor_date(df$DateTimeUTCmin5, unit = "hour")