我有很多动物的GPS数据.我需要计算每个人的步长,然后比较白天和晚上的平均步长.下面的代码只对一个人执行此操作,但我需要按每个人的ID循环遍历每个人.

更多的背景-我这样做是因为我不确定我正在使用的数据集的时间列是否已经转换为当地时间,所以我比较了白天和晚上之间的步长,认为这个特定的物种在白天应该不那么活跃.如果不是,那么时间可能是错误的.任何帮助是赞赏!

structure(list(`(Id)` = c("151_230_25_2", "151_230_25_2", "151_230_25_2", 
"151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
"151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
"151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
"151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
"151_230_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
"151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
"151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
"151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
"151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
"151_259_25_2", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
"151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
"151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
"151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
"151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
"151_259_45_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
"151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
"151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
"151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
"151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
"151_290_25_1", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
"151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
"151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
"151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
"151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
"151_290_75_2"), Date = structure(c(14197, 14197, 14197, 14197, 
14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 
14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 
14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 
14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 
13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 
13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 
13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 
13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 13831, 
13831, 13831, 13831, 13831, 14197, 14197, 14197, 14197, 14197, 
14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 14197, 
14197, 14197, 14197, 14197, 14197, 14197), class = "Date"), Time = structure(c(0, 
1800, 3600, 5400, 7200, 9060, 10800, 12600, 14400, 16200, 18000, 
19800, 21600, 23400, 25200, 27000, 28800, 30600, 32400, 34200, 
0, 1860, 3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 
18000, 19800, 21600, 23400, 25200, 27000, 28800, 30600, 32400, 
34200, 0, 1800, 3600, 5400, 7200, 9000, 10800, 12600, 14400, 
18060, 19860, 21600, 23400, 25200, 27000, 28800, 30600, 32400, 
34200, 36000, 0, 1800, 3600, 5460, 7200, 9000, 10800, 12600, 
14400, 16200, 18000, 19800, 21600, 23400, 25200, 27000, 28800, 
30660, 32400, 34200, 0, 1800, 3600, 5400, 7200, 9000, 10800, 
12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200, 27000, 
28800, 30600, 32400, 34200), class = c("hms", "difftime"), units = "secs"), 
    Id = c("151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
    "151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
    "151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
    "151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
    "151_230_25_2", "151_230_25_2", "151_230_25_2", "151_230_25_2", 
    "151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
    "151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
    "151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
    "151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
    "151_259_25_2", "151_259_25_2", "151_259_25_2", "151_259_25_2", 
    "151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
    "151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
    "151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
    "151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
    "151_259_45_1", "151_259_45_1", "151_259_45_1", "151_259_45_1", 
    "151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
    "151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
    "151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
    "151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
    "151_290_25_1", "151_290_25_1", "151_290_25_1", "151_290_25_1", 
    "151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
    "151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
    "151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
    "151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2", 
    "151_290_75_2", "151_290_75_2", "151_290_75_2", "151_290_75_2"
    ), DateTime = structure(c(1226620800, 1226622600, 1226624400, 
    1226626200, 1226628000, 1226629800, 1226631600, 1226633400, 
    1226635200, 1226637000, 1226638800, 1226640600, 1226642400, 
    1226644200, 1226646000, 1226647800, 1226649600, 1226651400, 
    1226653200, 1226655000, 1226620800, 1226622600, 1226624400, 
    1226626200, 1226628000, 1226629800, 1226631600, 1226633400, 
    1226635200, 1226637000, 1226638800, 1226640600, 1226642400, 
    1226644200, 1226646000, 1226647800, 1226649600, 1226651400, 
    1226653200, 1226655000, 1194998400, 1195000200, 1195002000, 
    1195003800, 1195005600, 1195007400, 1195009200, 1195011000, 
    1195012800, 1195016400, 1195018200, 1195020000, 1195021800, 
    1195023600, 1195025400, 1195027200, 1195029000, 1195030800, 
    1195032600, 1195034400, 1194998400, 1195000200, 1195002000, 
    1195003800, 1195005600, 1195007400, 1195009200, 1195011000, 
    1195012800, 1195014600, 1195016400, 1195018200, 1195020000, 
    1195021800, 1195023600, 1195025400, 1195027200, 1195029000, 
    1195030800, 1195032600, 1226620800, 1226622600, 1226624400, 
    1226626200, 1226628000, 1226629800, 1226631600, 1226633400, 
    1226635200, 1226637000, 1226638800, 1226640600, 1226642400, 
    1226644200, 1226646000, 1226647800, 1226649600, 1226651400, 
    1226653200, 1226655000), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), x = c(634284.1, 634309, 634408.2, 634375.1, 634311.2, 
    634284.1, 634280, 634287.1, 634282.3, 634279.5, 634345.9, 
    634467.1, 634642.7, 634778.7, 634777.9, 634914.8, 635110.4, 
    635051.1, 634817.2, 634493.2, 632791.5, 632789, 632779.1, 
    632779.2, 632783.7, 632784.7, 632945.4, 632873.2, 632881.8, 
    632914.5, 632902.8, 632880.3, 633005.9, 633126.4, 633178.4, 
    633229.4, 633235.5, 633167.2, 632827.3, 632792.4, 636092.752445996, 
    636085.71870334, 636085.960986795, 636080.209776018, 636067.492673929, 
    636070.703147982, 636067.727121061, 636072.25358048, 636067.733635585, 
    636022.254595604, 636049.378154663, 636052.774163599, 636044.281856078, 
    636020.825814475, 636017.501109539, 635998.576789699, 635984.944547542, 
    635995.97293996, 635994.65604081, 635997.441594901, 634117.793160993, 
    634123.560619043, 634126.79519071, 634139.270126081, 634123.493214937, 
    634129.086702961, 634120.172806562, 634103.476705276, 634114.211880121, 
    634122.412333279, 634147.110097123, 634153.311725244, 634149.622657568, 
    634145.54023752, 634157.538900614, 634176.936148175, 634039.024413502, 
    633994.511113991, 633927.544166519, 633955.807482225, 635746.9, 
    635766.5, 635747, 635748.1, 635750.8, 635749.8, 635665.6, 
    635583.2, 635601.3, 635607.8, 635603.5, 635617.4, 635654.3, 
    635610, 635601.7, 635590.9, 635588.4, 635572.7, 635575.4, 
    635556), y = c(3037674, 3037675, 3037625, 3037597, 3037676, 
    3037666, 3037666, 3037674, 3037666, 3037676, 3037676, 3037662, 
    3037554, 3037480, 3037487, 3037459, 3037446, 3037371, 3037266, 
    3037267, 3038537, 3038509, 3038499, 3038500, 3038504, 3038507, 
    3038609, 3038571, 3038578, 3038605, 3038596, 3038451, 3038321, 
    3038177, 3038169, 3038025, 3037925, 3037819, 3037734, 3037244, 
    3041712.62393865, 3041705.37714781, 3041680.88214683, 3041712.0858203, 
    3041710.34914796, 3041725.55318648, 3041711.4819015, 3041714.05836591, 
    3041708.22447917, 3041671.49952197, 3041727.53247622, 3041733.14336357, 
    3041731.78600236, 3041728.13525523, 3041728.56372061, 3041705.63993685, 
    3041691.98229541, 3041650.88737444, 3041640.44657043, 3041649.43003949, 
    3038454.51463208, 3038449.49203901, 3038450.14785299, 3038461.70743224, 
    3038450.23364733, 3038446.66060605, 3038450.19736362, 3038461.33845936, 
    3038488.30205007, 3038469.55604232, 3038439.77760359, 3038442.9588028, 
    3038435.96038282, 3038440.53603018, 3038445.03260561, 3038458.93924427, 
    3038432.65800031, 3038466.03173824, 3038456.74725178, 3038450.68481899, 
    3037382, 3037380, 3037439, 3037444, 3037441, 3037438, 3037436, 
    3037453, 3037497, 3037513, 3037512, 3037501, 3037198, 3037083, 
    3037073, 3037083, 3037099, 3037123, 3037101, 3037126)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -100L), groups = structure(list(
    `(Id)` = c("151_230_25_2", "151_259_25_2", "151_259_45_1", 
    "151_290_25_1", "151_290_75_2"), .rows = structure(list(1:20, 
        21:40, 41:60, 61:80, 81:100), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), .drop = TRUE))
library(tidyverse)
library(amt)
library(lubridate)
library(maptools)
library(suncalc)
library(ggplot2)
library(sp)

df2 <- df %>%
  mutate(Id=as.factor(Id),
         DateTime=as.POSIXct(DateTime, format="%m/%d/%Y %I:%M %p",tz='CST6CDT')) %>%
  drop_na(DateTime) %>%
  select(Id,Time, DateTime,x,y) %>%
  filter(Id=='151_259_45_1') %>%
  droplevels()
summary(df2, maxsum=20)

all(complete.cases(df2))

# check for duplicated time stamps
any(duplicated(df2$DateTime))

trk<-make_track(df2, x, y, DateTime, id = Id, crs = 4326, all_cols = T)
trk

trk <- trk %>%
  mutate(sl_ = step_lengths(trk))

summarize_sampling_rate(trk)

trk2<-track_resample(trk, rate=minutes(30))
trk2

stp2<-steps_by_burst(trk2)


stp2 <- stp2 %>%
  mutate(time = t1_-6*60*60,
         time=format(time, "%H:%M:%S"))

stp2 <- stp2 %>%
  mutate(time=format(t1_, "%H:%M:%S"))

stp3 <- stp2 %>%
    mutate(time_period=if_else(time > '06:30:00' & time < '19:30:00', "Day", "Night")) %>%
    group_by(time_period) %>%
    mutate(avg_sl=mean(sl_))

ggplot(stp3, aes(time_period, avg_sl)) + 
        geom_bar(stat='summary')

推荐答案

在R中不推荐使用for循环,因为它们往往很慢.首选方法是使用列表.我不确定我是否理解正确,但我认为你需要的是split()purrr::map()lapply().split()会将您的数据框分成一个数据框列表,每个数据框只包含一个ID.然后,map()将对每个数据框应用给定的函数.

df2 <- df %>%
  mutate(DateTime = as.POSIXct(DateTime, format = "%m/%d/%Y %I:%M %p", tz = "CST6CDT")) %>%
  drop_na(DateTime) %>%
  select(Id, Time, DateTime, x, y)

# Your code as a function
anaysis <- function(df) {
  description <- summary(df2, maxsum = 20)
  trk <- make_track(df2, x, y, DateTime, id = Id, crs = 4326, all_cols = T)
  trk <- trk %>%
    mutate(sl_ = step_lengths(trk))
  description2 <- summarize_sampling_rate(trk)
  trk2 <- track_resample(trk, rate = minutes(30))
  stp2 <- steps_by_burst(trk2)
  stp2 <- stp2 %>%
    mutate(
      time = t1_ - 6 * 60 * 60,
      time = format(time, "%H:%M:%S")
    )
  stp2 <- stp2 %>%
    mutate(time = format(t1_, "%H:%M:%S"))
  stp3 <- stp2 %>%
    mutate(time_period = if_else(time > "06:30:00" & time < "19:30:00", "Day", "Night")) %>%
    group_by(time_period) %>%
    mutate(avg_sl = mean(sl_))
  plot <- ggplot(stp3, aes(time_period, avg_sl)) +
    geom_bar(stat = "summary")
  lst(description, trk, description2, trk2, stp2, stp3, plot) # return a named list
}

df2 %>%
  split(df2$Id) %>% # mind the `$`
  purrr::map(anaysis)

R相关问答推荐

使用spatVector裁剪网格数据时出现的问题

在R中无法读入具有Readxl和lApply的数据集

如何在所有绘图中保持条件值的 colored颜色 相同?

将. xlsx内容显示为HTML表

如何提取所有完美匹配的10个核苷酸在一个成对的匹配与生物字符串在R?>

在GG图中绘制射线的自动程序

按多列统计频次

QY数据的处理:如何定义QY因素的水平

仅在R中的数据集开始和结束时删除所有 Select 列的具有NA的行

如何删除R中除数字元素以外的所有元素

解析嵌套程度极高的地理数据

计算来自单独分组的分幅的值的百分位数

Ggplot2如何找到存储在对象中的残差和拟合值?

是否从列中删除★符号?

如何获取R chromote中的当前URL?

在shiny 表格中输入的文本在第一次后未更新

通过比较来自多个数据框的值和R中的条件来添加新列

识别部分重复行,其中一行为NA,其重复行为非NA

根据向量对列表元素进行排序

真实世界坐标的逆st_变换