删除R中的工作日行

时间:2015-01-24 19:08:13

标签: r date

我的数据如下:

enter image description here

dput()

structure(list(time = structure(c(1L, 2L, 3L, 3L, 4L, 4L, 4L, 
4L, 5L, 6L, 7L, 8L, 9L, 9L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 
12L, 13L, 14L, 15L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 18L, 19L, 
20L, 21L, 22L, 22L, 23L, 24L, 25L, 25L, 25L, 25L, 25L, 26L, 27L, 
28L, 29L, 30L), .Label = c("2014-07-01 00:01:11", "2014-07-01 00:01:12", 
"2014-07-01 00:02:10", "2014-07-01 00:02:14", "2014-07-01 00:03:12", 
"2014-07-01 00:05:11", "2014-07-01 00:05:12", "2014-07-01 00:07:10", 
"2014-07-01 00:07:11", "2014-07-01 00:07:12", "2014-07-01 00:07:15", 
"2014-07-01 00:09:11", "2014-07-01 00:09:12", "2014-07-01 00:11:11", 
"2014-07-01 00:11:12", "2014-07-01 00:12:10", "2014-07-01 00:12:14", 
"2014-07-01 00:13:11", "2014-07-01 00:13:12", "2014-07-01 00:15:11", 
"2014-07-01 00:15:12", "2014-07-01 00:17:10", "2014-07-01 00:17:11", 
"2014-07-01 00:17:12", "2014-07-01 00:17:14", "2014-07-01 00:17:19", 
"2014-07-01 00:19:11", "2014-07-01 00:19:12", "2014-07-01 00:21:11", 
"2014-07-01 00:21:12"), class = "factor"), type = structure(c(1L, 
1L, 2L, 4L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 4L, 1L, 2L, 1L, 5L, 5L, 
5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 2L, 4L, 5L, 5L, 5L, 5L, 5L, 1L, 
1L, 1L, 1L, 2L, 4L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 3L, 1L, 1L, 1L, 
1L), .Label = c("activity", "battery", "bluetooth", "location", 
"wifi"), class = "factor"), value1 = structure(c(10L, 10L, 6L, 
2L, 11L, 7L, 8L, 1L, 10L, 10L, 10L, 4L, 10L, 6L, 10L, 11L, 7L, 
8L, 1L, 9L, 1L, 10L, 10L, 10L, 10L, 6L, 5L, 11L, 7L, 1L, 8L, 
1L, 10L, 10L, 10L, 10L, 6L, 3L, 10L, 10L, 11L, 7L, 8L, 1L, 11L, 
12L, 10L, 10L, 10L, 10L), .Label = c("", "35.1897751", "35.189777", 
"35.189782", "35.1897954", "discharging", "dlink", "iptime", 
"JIN iptime", "none", "PATECH-AP", "SHW-A300K(88**)"), class = "factor"), 
    value2 = structure(c(1L, 1L, 1L, 7L, 12L, 2L, 3L, 11L, 1L, 
    1L, 1L, 6L, 1L, 1L, 1L, 12L, 2L, 3L, 11L, 8L, 10L, 1L, 1L, 
    1L, 1L, 1L, 4L, 12L, 2L, 10L, 3L, 11L, 1L, 1L, 1L, 1L, 1L, 
    5L, 1L, 1L, 12L, 2L, 3L, 11L, 9L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "00:26:5a:2f:29:82", "00:26:66:68:ae:18", "126.9063212", 
    "126.9063384", "126.9063733", "126.9063876", "64:e5:99:71:40:96", 
    "7c:3e:9d:19:33:b1", "7c:3e:9d:19:33:b5", "7c:3e:9d:19:33:b6", 
    "7c:3e:9d:19:33:b7"), class = "factor")), .Names = c("time", 
"type", "value1", "value2"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
"47", "48", "49", "50"))

如果此示例不够,您可以通过this link

下载示例数据

我的问题是:

  1. 我想删除工作日,这意味着星期六和星期日的数据,因为 我只想使用星期一到星期五的数据。
  2. 我还有一个问题。我想只在一小时内改变时间 格式,例如:如果分钟小于30所以我想 更改为上一次,当分钟超过30我想要 四舍五入到下一次。如果时间< 12:30,时间结果是12:00 时间> 12:30,时间结果是13:00。实际上,我可以使用拆分和 然后使用if条件,但有更简单和有效 方式是什么?

1 个答案:

答案 0 :(得分:2)

你可以像这样过滤掉周末(周六和周日):
(假设您的data.frame被称为x

x[!weekdays(as.Date(x$time)) %in% c("Saturday", "Sunday"),]

或(如果您不是英语语言环境)

x[!as.POSIXlt(x$time)$wday %in% c(0, 6),]

您可以转换为POSIXct并使用round

来舍入到最近的小时
round(as.POSIXct(x$time), units="hours")