选择数据的小时数范围

时间:2017-09-19 08:53:08

标签: r

我是R的新手,我正在寻找一种方法来循环我的工作时间。

我希望例如在数据框中选择0h00和1h00之间的小时

在1h00-2h00之后的选定小时之后,直到最后做同样的事情

这是我从现在开始做的事情:

daily_data = read.csv("/U/k/Documents/eco.csv", header=TRUE, stringsAsFactors=FALSE)
    #copy our data frame
    daily_data2= daily_data
    daily_hour= daily_data$time

    #to get my hours
    library(lubridate)

    #to get the format  hour minut second
    daily_data2$time=format(ymd_hms(daily_hour), "%H:%M:%S")

    # get min and max
    min(daily_data2$time)
    max(daily_data2$time)
    times=daily_data2$time

    #order by time
    daily_data2= daily_data2[ order(daily_data2$time , decreasing = FALSE ),]

    #nnumber of rows
    nrow(daily_data2)

这是我的数据:

 [1] "00:30:51" "00:30:51" "00:30:51" "00:30:51" "00:30:51"
   [6] "00:30:51" "00:30:51" "00:30:51" "00:30:51" "00:30:51"
  [11] "00:57:46" "00:57:46" "00:57:46" "00:57:46" "00:57:46"
  [16] "00:57:46" "00:57:46" "00:57:46" "00:57:46" "00:57:46"
  [21] "00:57:46" "00:57:46" "00:57:46" "00:57:46" "00:57:46"
  [26] "00:57:46" "00:57:46" "00:57:46" "00:57:46" "00:57:46"
  [31] "01:01:42" "01:01:42" "01:01:42" "01:01:42" "01:01:42"
  [36] "01:01:42" "01:01:42" "01:41:50" "01:41:50" "01:41:50"
  [41] "01:41:50" "01:41:50" "01:41:50" "01:41:50" "01:41:50"
  [46] "01:41:50" "01:41:50" "01:41:50" "01:41:50" "01:41:50"
  [51] "01:54:32" "01:54:32" "01:54:32" "01:54:32" "01:54:32"
  [56] "01:54:32" "01:54:32" "02:04:40" "02:04:40" "02:04:40"
  [61] "02:04:40" "02:04:40" "02:04:40" "02:04:40" "02:04:40"
  [66] "02:04:40" "02:04:40" "02:04:40" "02:04:40" "02:04:40"
  [71] "02:04:40" "02:04:40" "02:04:40" "02:04:40" "02:04:40"
  [76] "02:04:40" "02:04:40" "02:04:40" "02:04:40" "02:04:40"
  [81] "02:04:40" "02:04:40" "02:04:40" "02:04:40" "02:40:41"
  [86] "02:40:41" "02:40:41" "02:40:41" "02:51:17" "02:51:17"
  [91] "02:51:17" "02:51:17" "02:51:17" "02:51:17" "02:51:17"
  [96] "02:51:17" "02:51:17" "02:51:17" "03:36:38" "03:36:38"
 [101] "03:36:38" "03:36:38" "04:06:30" "04:06:30" "04:06:30"
 [106] "04:06:30" "04:06:30" "04:06:30" "04:06:30" "04:06:30"
 [111] "04:41:21" "04:41:21" "04:41:21" "04:41:21" "04:46:08"
 [116] "04:46:08" "04:46:08" "04:46:08" "04:46:08" "04:46:08"
 [121] "04:46:08" "04:46:08" "04:46:08" "04:46:08" "04:46:08"
 [126] "04:48:17" "04:48:17" "04:48:17" "04:48:17" "04:48:17"
 [131] "04:48:17" "04:48:17" "05:04:21" "05:04:21" "05:04:21"
 [136] "05:04:21" "05:04:21" "05:04:21" "05:04:21" "05:04:21"
 [141] "05:35:54" "05:35:54" "05:35:54" "05:35:54" "05:40:34"
 [146] "05:40:34" "05:40:34" "05:40:34" "05:59:41" "05:59:41"


dput():

structure(list(visitorID = c(16081523021881100, 16081523021881100, 
16081523021881100, 16081523021881100, 16082009423468440, 16082009423468440, 
16082009423468440, 16082009423468440, 16082011015136602, 16082011015136602, 
16082011015136602, 16082011015136602, 16042501530940622, 16042501530940622, 
16042501530940622, 16042501530940622, 16042501530940622, 16042501530940622, 
16042501530940622, 16081915031276100, 16081915031276100, 16081915031276100, 
16081915031276100, 16082011544595760, 16082011544595760, 16082011544595760, 
16082011544595760, 15121218335405640, 15121218335405640, 15121218335405640, 
15121218335405640, 16070618525948140, 16070618525948140, 16070618525948140, 
16070618525948140, 16082013260190662, 16082013260190662, 16082013260190662, 
16082013260190662, 15122122362858740, 15122122362858740, 15122122362858740, 
15122122362858740, 15122122362858740, 15122122362858740, 15122122362858740, 
15122122362858740, 15122122362858740, 15122122362858740, 16072917490265320, 
16072917490265320, 16072917490265320, 16072917490265320, 16072917490265320, 
16072917490265320, 16072917490265320, 16080917544867900, 16080917544867900, 
16080917544867900, 16080917544867900, 16082013300896342, 16082013300896342, 
16082013300896342, 16082013300896342, 16071621581562760, 16071621581562760, 
16071621581562760, 16071621581562760, 16082010060486120, 16082010060486120, 
16082010060486120, 16082010060486120, 16081914082559062, 16081914082559062, 
16081914082559062, 16081914082559062, 16081914082559062, 16081914082559062, 
16081914082559062, 16082013524044222, 16082013524044222, 16082013524044222, 
16082013524044222, 16082013524044222, 16082013524044222, 16082013524044222, 
16082014181501102, 16082014181501102, 16082014181501102, 16082014181501102, 
16081522070819900, 16081522070819900, 16081522070819900, 16081522070819900, 
16082010581119800, 16082010581119800, 16082010581119800, 16082010581119800, 
16082010581119800, 16082010581119800, 16082010581119800, 16082010581119800, 
16082010581119800, 16082010581119800, 16082010581119800, 16082010581119800, 
16082010581119800, 16082010581119800, 16082014590598140, 16082014590598140, 
16082014590598140, 16082014590598140, 16081721082864120, 16081721082864120, 
16081721082864120, 16081721082864120, 16081121413933360, 16081121413933360, 
16081121413933360, 16081121413933360, 16082018253153680, 16082018253153680, 
16082018253153680, 16082018253153680, 16081918503913360, 16081918503913360, 
16081918503913360, 16081918503913360, 16060510333974900, 16060510333974900, 
16060510333974900, 16060510333974900, 16071313381189080, 16071313381189080, 
16071313381189080, 16071313381189080, 16071313381189080, 16071313381189080, 
16081414411464480, 16081414411464480, 16081414411464480, 16081414411464480, 
16081414411464480, 16081414411464480, 16081414411464480, 16081414411464480, 
16081414411464480, 16081414411464480, 16081414411464480, 16081920380127900, 
16081920380127900, 16081920380127900, 16081920380127900, 16081920380127900, 
16081920380127900, 16081920380127900, 16081920380127900, 16082014203957940, 
16082014203957940, 16082014203957940, 16082014203957940, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16060409004090500, 
16060409004090500, 16060409004090500, 16060409004090500, 16081921092401600, 
16081921092401600, 16081921092401600, 16081921092401600, 16081921092401600, 
16081921092401600, 16082021431422460, 16082021431422460, 16082021431422460, 
16082021431422460, 16082021431422460, 16082021431422460, 16082021431422460, 
16082021431422460, 16041014505621220, 16041014505621220, 16041014505621220, 
16041014505621220), variationID = c(190949L, 190949L, 190949L, 
190949L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 0L, 0L, 0L, 0L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 190949L, 190949L, 
190949L, 190949L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L, 0L, 0L, 0L, 0L, 0L, 0L, 190949L, 190949L, 190949L, 190949L, 
190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 190949L, 
190949L), categoryID = c(279L, 280L, 281L, 282L, 279L, 280L, 
281L, 282L, 279L, 280L, 281L, 282L, 279L, 281L, 282L, 279L, 280L, 
281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 
280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 
279L, 280L, 282L, 279L, 280L, 282L, 279L, 280L, 281L, 282L, 279L, 
280L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 
280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 
279L, 280L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 282L, 279L, 
280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 
279L, 280L, 282L, 279L, 280L, 282L, 279L, 280L, 281L, 282L, 279L, 
280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 
279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 
282L, 279L, 280L, 281L, 282L, 279L, 280L, 279L, 280L, 281L, 282L, 
279L, 280L, 281L, 282L, 279L, 280L, 282L, 279L, 280L, 281L, 282L, 
279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 
282L, 279L, 280L, 281L, 282L, 279L, 280L, 279L, 280L, 282L, 279L, 
280L, 281L, 282L, 279L, 280L, 282L, 279L, 280L, 282L, 279L, 280L, 
282L, 279L, 280L, 282L, 279L, 280L, 281L, 279L, 280L, 281L, 282L, 
279L, 280L, 279L, 280L, 281L, 282L, 279L, 280L, 281L, 282L, 279L, 
280L, 281L, 282L, 279L, 280L, 281L, 282L), actionID = c(185973L, 
0L, 115960L, 136482L, 875336L, 875337L, 115960L, 125404L, 149006L, 
149007L, 115960L, 116629L, 513178L, 116586L, 156879L, 241303L, 
241304L, 115960L, 123037L, 282869L, 282870L, 115960L, 124275L, 
149341L, 149342L, 117481L, 149343L, 146691L, 146692L, 115960L, 
130179L, 1591330L, 1591331L, 115960L, 1591332L, 155040L, 155041L, 
116589L, 151320L, 159247L, 159248L, 122574L, 994385L, 994386L, 
129246L, 215667L, 215668L, 115960L, 122183L, 173664L, 173665L, 
132103L, 149131L, 149132L, 115960L, 1049804L, 127747L, 127748L, 
123518L, 117617L, 417633L, 417634L, 116586L, 119878L, 214544L, 
214545L, 115960L, 123037L, 477342L, 477343L, 115960L, 116520L, 
1044828L, 971958L, 148657L, 231612L, 231613L, 115960L, 119764L, 
119759L, 119760L, 124439L, 156135L, 156136L, 115960L, 121891L, 
160936L, 160937L, 115960L, 122923L, 192107L, 192108L, 115960L, 
125404L, 153385L, 153386L, 229503L, 153387L, 153388L, 122923L, 
195047L, 195048L, 116586L, 116744L, 410865L, 1064038L, 115960L, 
130179L, 229747L, 229748L, 115960L, 119761L, 147634L, 147635L, 
116586L, 120693L, 411773L, 411774L, 115960L, 125639L, 970543L, 
970544L, 115960L, 161392L, 148156L, 148157L, 152184L, 221911L, 
134953L, 134954L, 115960L, 121939L, 529644L, 529645L, 213268L, 
213269L, 115960L, 177716L, 130070L, 130071L, 116586L, 116629L, 
182608L, 182609L, 118918L, 1158585L, 1158586L, 115960L, 127321L, 
838346L, 838347L, 116586L, 121437L, 214544L, 214545L, 115960L, 
123037L, 153437L, 153438L, 115960L, 121437L, 186390L, 186391L, 
135167L, 117480L, 163009L, 163010L, 843566L, 843567L, 221911L, 
163011L, 163012L, 116589L, 974599L, 121435L, 121436L, 121437L, 
121438L, 121439L, 119301L, 1356321L, 1356322L, 122091L, 1604110L, 
1604111L, 125883L, 128694L, 128695L, 116586L, 128696L, 128697L, 
115960L, 985266L, 149591L, 149592L, 337193L, 337194L, 115960L, 
116590L, 890146L, 890147L, 116589L, 118695L, 127031L, 127032L, 
115960L, 116592L, 125550L, 125551L, 115960L, 121891L), time = c("07:13:32", 
"07:13:32", "07:13:32", "07:13:32", "08:52:12", "08:52:12", "08:52:12", 
"08:52:12", "09:10:09", "09:10:09", "09:10:09", "09:10:09", "09:44:15", 
"09:44:15", "09:44:15", "09:44:15", "09:44:15", "09:44:15", "09:44:15", 
"10:06:53", "10:06:53", "10:06:53", "10:06:53", "10:06:56", "10:06:56", 
"10:06:56", "10:06:56", "11:06:56", "11:06:56", "11:06:56", "11:06:56", 
"11:33:07", "11:33:07", "11:33:07", "11:33:07", "11:34:49", "11:34:49", 
"11:34:49", "11:34:49", "11:46:26", "11:46:26", "11:46:26", "11:46:26", 
"11:46:26", "11:46:26", "11:46:26", "11:46:26", "11:46:26", "11:46:26", 
"12:00:06", "12:00:06", "12:00:06", "12:00:06", "12:00:06", "12:00:06", 
"12:00:06", "12:02:39", "12:02:39", "12:02:39", "12:02:39", "12:04:14", 
"12:04:14", "12:04:14", "12:04:14", "12:26:47", "12:26:47", "12:26:47", 
"12:26:47", "12:45:18", "12:45:18", "12:45:18", "12:45:18", "12:45:55", 
"12:45:55", "12:45:55", "12:45:55", "12:45:55", "12:45:55", "12:45:55", 
"12:52:45", "12:52:45", "12:52:45", "12:52:45", "12:52:45", "12:52:45", 
"12:52:45", "12:57:32", "12:57:32", "12:57:32", "12:57:32", "12:57:35", 
"12:57:35", "12:57:35", "12:57:35", "15:18:51", "15:18:51", "15:18:51", 
"15:18:51", "15:18:51", "15:18:51", "15:18:51", "15:18:51", "15:18:51", 
"15:18:51", "15:18:51", "15:18:51", "15:18:51", "15:18:51", "15:21:04", 
"15:21:04", "15:21:04", "15:21:04", "15:35:34", "15:35:34", "15:35:34", 
"15:35:34", "16:56:29", "16:56:29", "16:56:29", "16:56:29", "17:16:52", 
"17:16:52", "17:16:52", "17:16:52", "17:45:19", "17:45:19", "17:45:19", 
"17:45:19", "18:07:44", "18:07:44", "18:07:44", "18:07:44", "18:41:33", 
"18:41:33", "18:41:33", "18:41:33", "18:41:33", "18:41:33", "18:44:38", 
"18:44:38", "18:44:38", "18:44:38", "18:44:38", "18:44:38", "18:44:38", 
"18:44:38", "18:44:38", "18:44:38", "18:44:38", "18:59:19", "18:59:19", 
"18:59:19", "18:59:19", "18:59:19", "18:59:19", "18:59:19", "18:59:19", 
"19:13:14", "19:13:14", "19:13:14", "19:13:14", "19:22:55", "19:22:55", 
"19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", 
"19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", 
"19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", 
"19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", 
"19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", "19:22:55", 
"19:27:07", "19:27:07", "19:27:07", "19:27:07", "19:27:07", "19:27:07", 
"19:57:19", "19:57:19", "19:57:19", "19:57:19", "19:57:19", "19:57:19", 
"19:57:19", "19:57:19", "23:33:46", "23:33:46", "23:33:46", "23:33:46"
)), .Names = c("visitorID", "variationID", "categoryID", "actionID", 
"time"), row.names = c(23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 
44L, 45L, 46L, 186L, 187L, 188L, 189L, 47L, 48L, 49L, 50L, 51L, 
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 
65L, 66L, 67L, 190L, 191L, 192L, 193L, 68L, 69L, 70L, 71L, 72L, 
73L, 74L, 75L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 
202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L, 76L, 
77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 
90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 
102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 1L, 2L, 3L, 4L, 
110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 
121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 131L, 132L, 133L, 134L, 135L, 
136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 
147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 
158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 13L, 14L, 
15L, 16L, 17L, 18L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 
174L, 19L, 20L, 21L, 22L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

您可以沿着这些方向行事(使用示例数据作为df):

library(lubridate)
df$time = hms(df$time)   # Convert to a time class
df$hour = hour(df$time)  # Extract only the hour component
df[df$hour == 7,]        # Perform subsetting
      visitorID variationID categoryID actionID       time hour
23 1.608152e+16      190949        279   185973 7H 13M 32S    7
24 1.608152e+16      190949        280        0 7H 13M 32S    7
25 1.608152e+16      190949        281   115960 7H 13M 32S    7
26 1.608152e+16      190949        282   136482 7H 13M 32S    7

解决方案的核心是将您的角色时间表示转换为实际时间戳。之后,R提供了许多工具,例如来自lubridate的hour函数来提取小时组件。