简体   繁体   English

使用cut函数时breaks参数出错

[英]Error on breaks argument when using the cut function

I'm learning R and I need to create a two way table from the data below: 我正在学习R,需要从以下数据创建双向表:

> head(datanet)
   Date & Time [Local]  distance travelled
1:    18/06/2018 03:08  15.959366
2:    18/06/2018 03:12  22.535566
3:    18/06/2018 03:16  12.036834
4:    18/06/2018 03:20  18.738134
5:    18/06/2018 03:24  26.781879
6:    18/06/2018 03:28  8.341659

My desired output should look like the table below, having an hour column entry representing the time of the day (24 entries for the 24 hours of the day) and several dist_tra on yyyy-mm-dd entries with the average hourly distance travelled per day for each hour of the day. 我所需的输出应如下表所示,其中有一个hour列条目代表一天中的时间(一天中的24小时有24个条目), dist_tra on yyyy-mm-dd条目中有多个dist_tra on yyyy-mm-dd每天的平均小时行驶距离一天中的每个小时。 Like so: 像这样:

head(dist.byHour[1:3])
  hour dist_tra on 06/07/2018  dist_tra on 06/08/2018
1:   00              25.834355              29.388140
2:   01                     NA               8.329956
3:   02                     NA              31.506390
4:   03              33.464954              20.995957
5:   04               6.406513              17.035749
6:   05              28.254438              38.803171

By looking online and talking to some collegues I got the script below. 通过在线查找并与一些同事交谈,我获得了以下脚本。 However, I'm having an undesired error message when using cut() : 但是,使用cut()时出现错误消息:

library(tidyverse)

datanet$datehour <- cut(datanet[[1]], breaks = "hours")

dist.byHour <- aggregate(meters ~ datehour, datanet, mean, na.rm = TRUE)
dist.byHour$datehour <- as.POSIXct(dist.byHour$datehour)
dist.byHour$hour <- format(dist.byHour$datehour, "%H")
dist.byHour$datehour <- as.Date(dist.byHour$datehour)
dist.byHour <- dist.byHour[c(3, 1, 2)]

dist.byHour <- dist.byHour %>%
  spread(datehour, -hour)

names(dist.byHour)[-1] <- paste("dist_tra on", names(dist.byHour)[-1])

The error being: 错误是:

> datanet$datehour <- cut(datanet[[1]], breaks = "hours")
Error in cut.default(datanet[[1]], breaks = "hours") : 
  'x' must be numeric

Any ideas on how to fix this? 有想法该怎么解决这个吗? This is an important assignment I'm working on so any help would be very appreciated! 这是我正在从事的一项重要任务,因此我们将不胜感激!

Actually there's no need to use cut, you can just use groups: 实际上,不需要使用剪切,您可以只使用组:

library(lubridate)
library(tidyverse)

# sample data
date <- c("18/06/2018 03:08", "18/06/2018 03:12", "18/06/2018 04:20", "19/06/2018 03:16", "19/06/2018 03:20", "19/06/2018 04:20")
distance <- c(15.959366,  22.535566, 12.036834,  18.738134, 12.036834, 22.535566)

df <- data.frame(date, distance)

df %>% 
  mutate(date = dmy_hm(date)) %>% #coerce to date object
  group_by(day = date(date), hour = hour(date)) %>% # group by day and hour
  summarise(dist = mean(distance)) %>% # average distance traveled in that hour
  spread(day, dist) # re-arrange dataframe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM