将日期和时间列合并为dd/mm/yyyy hh:mm [英] Combining date and time columns into dd/mm/yyyy hh:mm

查看:228
本文介绍了将日期和时间列合并为dd/mm/yyyy hh:mm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R和R工作室的新手. 我已经在五个单独的列中导入了日期和时间的数据集: YYYY MM DD hh mm 如何将它们合并为dd/mm/yyyy hh:mm格式的单个日期列?

New to R and R studio. I have imported a dataset with the date and time in five seperate columns headed: YYYY MM DD hh mm How do I combine these into a single date column of the format dd/mm/yyyy hh:mm?

数据集:

推荐答案

基于新信息以及@LauraKate在评论中提出的问题.

Based on the new information and questions asked by @LauraKate in the comment.

下面的答案是使用软件包dplyrggplot2进行的:

Answers below are done with packages dplyr and ggplot2:

df <- read.table("http://www.ndbc.noaa.gov/data/realtime2/51206.txt")
names(df) <- c("YYYY", "MM", "DD", "hh", "mm", "WD", "WSPD", "V1", "GST", "WVHT", "DPD", "APD", "MWD", "BARO", "ATMP", "V2", "V3", "V4", "V5")


df2 <- df %>% 
  unite(date, YYYY, MM, DD, sep="-") %>% 
  unite(time, hh, mm, sep=":") %>% 
  mutate(timestamp=paste(date, time) %>% 
           as.POSIXct(., format="%Y-%m-%d %H:%M")) %>% 
  select(timestamp, ATMP) 

ggplot(df2, aes(timestamp, ATMP)) + geom_line()

要获取每日最高温度超过26度(C)的警告点?

To get warning points when daily max temperature is above 26 degree (C)?

df3 <- df2 %>% 
    mutate(date=as.Date(timestamp) %>% as.POSIXct) %>% 
    group_by(date) %>% summarise(temp=max(ATMP)) %>% 
    mutate(warnings = ifelse(temp > 26, "red", "black"))

ggplot(df2, aes(timestamp, ATMP)) + 
    geom_line() + 
    geom_point(data=df3 %>% filter(warnings=="red"), aes(date, y=27.5), color="red")

这篇关于将日期和时间列合并为dd/mm/yyyy hh:mm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆