我有时间格式为m / d / Y H / m / s的数据。由于PosixIt错误,我无法执行mutate功能。请帮忙。
我的数据:
structure(list(time = "9/26/2017 6:19:00", panel = 230, mpp = 2.68627), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))
我的代码:
library(dplyr)
library(ggplot2)
data2%>% #filtering 230 and 231
select(time,panel,mpp)%>%
filter(data2,panel=="230")
filter(data2,panel=="231")
data_230<-filter(data2,panel=="230")
data_231<-filter(data2,panel=="231")
data_230
data_231
install.packages("lubridate")
library(lubridate)
#data_230$ts <- strptime(data_230$time, "%m/%d/%Y %H:%M:%S")
#data_230$tsf <- ymd_hms(data_230$ts)
library(dplyr)
#data_230%>%
data_230$ts <- as.POSIXct(strptime(data_230$time, "%m/%d/%Y %H:%M:%S"))
data_230$tsf <- ymd_hms(data_230$ts)
data_230 %>% mutate(ts=strptime(time, "%m/%d/%Y %H:%M:%S"),tsf=ymd_hms(ts)) %>% mutate(datehour= format(time,"%m-%d-%Y %H"), date1= format(time,"%m-%d-%Y"), month=format(time,"%m-%Y")) %>%
group_by(datehour) %>% mutate(hourlyP=mean(mpp)) %>% distinct(datehour, .keep_all = TRUE) %>%
group_by(date1) %>% mutate(dailyP=sum(hourlyP)) %>% distinct(date1, .keep_all = TRUE) %>%
group_by(month) %>% summarise(monthlyP=sum(dailyP))
答案 0 :(得分:1)
dplyr
函数可以最好地将日期时间数据作为POSIXct
对象处理。因此,我建议您将代码修改如下:
data_230 <- filter(data2, panel=="230")
data_230$time <- as.POSIXct(strptime(
data_230$time,
"%m/%d/%Y %H:%M:%S")
) # convert your character column to POSIXct object
# then the rest of your code:
data_230 %>%
mutate(ts = as.POSIXct(strptime(time, "%m/%d/%Y %H:%M:%S"),
tsf = ymd_hms(ts))) %>%
mutate(datehour = format(time,"%m-%d-%Y %H"),
date1 = format(time,"%m-%d-%Y"),
month = format(time,"%m-%Y")) %>%
group_by(datehour) %>%
mutate(hourlyP=mean(mpp)) %>%
distinct(datehour, .keep_all = TRUE) %>%
group_by(date1) %>%
mutate(dailyP=sum(hourlyP)) %>%
distinct(date1, .keep_all = TRUE) %>%
group_by(month) %>%
summarise(monthlyP=sum(dailyP))
尽管我真的不明白为什么需要ts
和tsf
,但是代码会运行并产生以下输出:
# A tibble: 1 x 2
month monthlyP
<chr> <dbl>
1 09-2017 2.69