我的数据框格式如下:
df$timestamp <- as.POSIXct(df$timestamp,format="%Y-%m-%d %H:%M:%S", tz="America/New_York")
我将时间戳转换为POSIX 的方式如下:
timestamp value
2014-02-14 14:27:00 "2014-02-14 14:27:00" "51.8460"
2014-02-14 14:32:00 "2014-02-14 14:32:00" "44.5080"
2014-02-14 14:37:00 "2014-02-14 14:37:00" "41.2440"
2014-02-14 14:42:00 "2014-02-14 14:42:00" "48.5680"
2014-02-14 14:47:00 "2014-02-14 14:47:00" "46.7140"
2014-02-14 14:52:00 "2014-02-14 14:52:00" "44.986"
之后,当我打印数据框的内容时,它似乎是这样的:
df <- read.csv(csv_filename, stringsAsFactors=FALSE)
df$timestamp <- as.POSIXct(df$timestamp,format="%Y-%m-%d %H:%M:%S", tz="America/New_York")
s <- xts(df, order.by = df$timestamp) #time series conversion
我正在使用的代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
@Injectable()
export class SwPeopleService {
people$ = this.http.get('https://swapi.co/api/people/')
.map((res:any) => res.results);
constructor(private http: HttpClient) {}
}
我不确定为什么xts函数会复制时间戳。
答案 0 :(得分:1)
通常(通常是?),时间序列在向量上。完成data.frame
后,它会以静默方式转换为matrix
,在这种情况下,会导致所有内容都转换为character
(即使是POSIXct)。
str(xts(df, order.by=d$timestamp))
# An 'xts' object on 2014-02-14 14:27:00/2014-02-14 14:47:00 containing:
# Data: chr [1:5, 1:2] "2014-02-14 14:27:00" "2014-02-14 14:32:00" "2014-02-14 14:37:00" ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:2] "timestamp" "value"
# Indexed by objects of class: [POSIXct,POSIXt] TZ: America/New_York
# xts Attributes:
# NULL
(请注意,数据为chr
或character
。)
相反,请使用xts(df$value, order.by=df$timestamp)
,因为它会保留数字类:
str(xts(df$value, order.by=df$timestamp))
# An 'xts' object on 2014-02-14 14:27:00/2014-02-14 14:47:00 containing:
# Data: num [1:5, 1] 51.8 44.5 41.2 48.6 46.7
# Indexed by objects of class: [POSIXct,POSIXt] TZ: America/New_York
# xts Attributes:
# NULL