使用一列的一部分在R中子集数据集

时间:2019-02-18 21:03:57

标签: r csv subset

要详细说明我的上一个问题:我想在R中为大型csv数据集子集。我想获取“ timestamp”列的信息,仅提取7pm到12am(含)之间的时间间隔。下面是数据示例:

Deer ID    TimeStamp         Location
1          4/16/18 12:00AM   DMA 1
2          4/16/18 3:00AM    DMA 1
3          4/16/18 9:30AM    DMA 2
4          4/16/18 7:00PM    DMA 1
5          4/16/18 8:30PM    DMA 2
6          4/16/18 11:00PM   DMA 3
7          4/17/18 1:30AM    DMA 2
8          4/17/18 5:00AM    DMA 1
9          4/17/18 9:00PM    DMA 3
10         4/17/18 11:30PM   DMA 1
11         4/18/18 12:30AM   DMA 2

所以我的最终目标是使自己达到以下目标:

Deer ID     TimeStamp        Location
4           4/16/18 7:00PM   DMA 1
5           4/16/18 8:30PM   DMA 2
6           4/16/18 11:00PM  DMA 3
9           4/17/18 9:00PM   DMA 3
10          4/17/18 11:30PM  DMA 1

关于如何实现此目标的任何想法?谢谢!

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作

# Convert TimeStamp to POSIXct 
df <- transform(df, TimeStamp = strptime(TimeStamp, "%m/%d/%Y %I:%M%p"))

# Use lubridate::hour to extract the hours from the POSIXct timestamp
library(lubridate)
df[(hour(df$TimeStamp) >= 19 & hour(df$TimeStamp) <= 24), ]
#   Deer.ID           TimeStamp Location
#4        4 0018-04-16 19:00:00    DMA 1
#5        5 0018-04-16 20:30:00    DMA 2
#6        6 0018-04-16 23:00:00    DMA 3
#9        9 0018-04-17 21:00:00    DMA 3
#10      10 0018-04-17 23:30:00    DMA 1

样本数据

df <- read.table(text =
    "'Deer ID'    TimeStamp         Location
1          '4/16/18 12:00AM'   'DMA 1'
2          '4/16/18 3:00AM'    'DMA 1'
3          '4/16/18 9:30AM'    'DMA 2'
4          '4/16/18 7:00PM'    'DMA 1'
5          '4/16/18 8:30PM'    'DMA 2'
6          '4/16/18 11:00PM'   'DMA 3'
7          '4/17/18 1:30AM'    'DMA 2'
8          '4/17/18 5:00AM'    'DMA 1'
9          '4/17/18 9:00PM'    'DMA 3'
10         '4/17/18 11:30PM'   'DMA 1'
11         '4/18/18 12:30AM'   'DMA 2'", header = T)

答案 1 :(得分:0)

tidyverse方式将遵循以下原则:

library(dplyr)
df <- read.table(
  text =
    "id    timestamp         location
                 1          '4/16/18 12:00AM'   'DMA 1'
                 2          '4/16/18 3:00AM'    'DMA 1'
                 3          '4/16/18 9:30AM'    'DMA 2'
                 4          '4/16/18 7:00PM'    'DMA 1'
                 5          '4/16/18 8:30PM'    'DMA 2'
                 6          '4/16/18 11:00PM'   'DMA 3'
                 7          '4/17/18 1:30AM'    'DMA 2'
                 8          '4/17/18 5:00AM'    'DMA 1'
                 9          '4/17/18 9:00PM'    'DMA 3'
                 10         '4/17/18 11:30PM'   'DMA 1'
                 11         '4/18/18 12:30AM'   'DMA 2'",
  header = TRUE
) %>%
  as_tibble()

df %>%
  mutate(timestamp = as.POSIXct(strptime(.data$timestamp, "%m/%d/%Y %I:%M%p"))) %>%
  filter(between(lubridate::hour(.data$timestamp), 19, 24))
#> # A tibble: 5 x 3
#>      id timestamp           location
#>   <int> <dttm>              <chr>   
#> 1     4 0018-04-16 19:00:00 DMA 1   
#> 2     5 0018-04-16 20:30:00 DMA 2   
#> 3     6 0018-04-16 23:00:00 DMA 3   
#> 4     9 0018-04-17 21:00:00 DMA 3   
#> 5    10 0018-04-17 23:30:00 DMA 1

reprex package(v0.2.1)于2019-02-19创建