2012-05-21 12 views
8

Ho un vettore DateTime all'interno di un data.frame in cui il frame di dati è costituito da 8760 osservazioni che rappresentano intervalli orari durante l'anno, ad es.ottenere ora dal vettore DateTime

2010-01-01 00:00 
2010-01-01 01:00 
2010-01-01 02:00 
2010-01-01 03:00 

e così via.

Vorrei creare un data.frame che abbia il vettore DateTime originale come prima colonna e quindi i valori orari nella seconda colonna, ad es.

2010-01-01 00:00   00:00 
2010-01-01 01:00   01:00 

Come può essere ottenuto?

risposta

11

Utilizzare format o strptime per estrarre le informazioni sull'ora.

Creare un vettore POSIXct:

x <- seq(as.POSIXct("2012-05-21"), by=("+1 hour"), length.out=5) 

Estrarre il tempo:

data.frame(
    date=x, 
    time=format(x, "%H:%M") 
) 

       date time 
1 2012-05-21 00:00:00 00:00 
2 2012-05-21 01:00:00 01:00 
3 2012-05-21 02:00:00 02:00 
4 2012-05-21 03:00:00 03:00 
5 2012-05-21 04:00:00 04:00 

Se il vettore di ingresso è un vettore di carattere, allora dovete convertire in POSIXct prima:

Creare alcuni dati

dat <- data.frame(
    DateTime=format(seq(as.POSIXct("2012-05-21"), by=("+1 hour"), length.out=5), format="%Y-%m-%d %H:%M") 
) 
dat 
      DateTime 
1 2012-05-21 00:00 
2 2012-05-21 01:00 
3 2012-05-21 02:00 
4 2012-05-21 03:00 
5 2012-05-21 04:00 

tempo Split out:

data.frame(
    DateTime=dat$DateTime, 
    time=format(as.POSIXct(dat$DateTime, format="%Y-%m-%d %H:%M"), format="%H:%M") 
) 

      DateTime time 
1 2012-05-21 00:00 00:00 
2 2012-05-21 01:00 01:00 
3 2012-05-21 02:00 02:00 
4 2012-05-21 03:00 03:00 
5 2012-05-21 04:00 04:00 
+0

Il vettore datetime è importato in r da un file di testo, quindi non ho semplicemente il vettore in formato AAAA-gg-mm ma come AAAA-gg-mm HH: MM quindi, sostituendo il tempo sopra con il mio vettore non funzionerà. Come convertire il vettore che ho nel formato YYYY-mm-gg HH: MM in un vettore POSIXct? – user1407388

+0

usa as.POSIXct e stipula il formato come argomentazione. vedere? as.POSIXct – JackeJR

+0

Ho provato x <- as.POSIXct (DateTime, format = "% Y-% m-% d% H:% M") ma restituisce un errore: Errore in as.POSIXct.default (DateTime, format = "% Y-% m-% d% H:% M"): non so come convertire 'DateTime' in classe "POSIXct" – user1407388

2

O genericamente, non trattandoli come date, è possibile utilizzare il seguente a condizione che il tempo e le date sono imbottiti in modo corretto.

library(stringr) 
df <- data.frame(DateTime = c("2010-01-01 00:00", "2010-01-01 01:00", "2010-01-01 02:00", "2010-01-01 03:00")) 
df <- data.frame(df, Time = str_sub(df$DateTime, -5, -1)) 

Dipende davvero dalle vostre esigenze.

2

Utilizzando lubridate

library(stringr) 
library(lubridate) 
library(plyr) 

df <- data.frame(DateTime = c("2010-01-01 00:00", "2010-01-01 01:00", "2010-01-01 02:00", "2010-01-01 03:00")) 

df <- mutate(df, DateTime = ymd_hm(DateTime), 
       time = str_c(hour(DateTime), str_pad(minute(DateTime), 2, side = 'right', pad = '0'), sep = ':')) 
Problemi correlati