2017-10-18 2 views
1

Ich habe einige Daten, die aus einer Excel-CSV-Datei importiert wurde.So ändern Sie importierte CSV in Xts in R

Wie bekomme ich diese Daten als xts?

  Date Open High  Low Close 
1 2017-09-01 1.29360 1.29951 1.29052 1.29495 
2 2017-09-03 1.29600 1.29660 1.29444 1.29535 
3 2017-09-04 1.29536 1.29643 1.29124 1.29300 
4 2017-09-05 1.29306 1.30440 1.29088 1.30331 
5 2017-09-06 1.30331 1.30820 1.30181 1.30471 
6 2017-09-07 1.30472 1.31381 1.30325 1.31319 
7 2017-09-08 1.31319 1.32239 1.31233 1.31945 
8 2017-09-10 1.31816 1.31978 1.31736 1.31769 
9 2017-09-11 1.31769 1.32223 1.31593 1.31764 
10 2017-09-12 1.31766 1.33145 1.31703 1.33097 
11 2017-09-13 1.33096 1.33286 1.31836 1.32040 
12 2017-09-14 1.32039 1.34055 1.31501 1.33955 
13 2017-09-15 1.33955 1.36159 1.33922 1.35871 
14 2017-09-17 1.35579 1.36029 1.35579 1.35921 
15 2017-09-18 1.35921 1.36181 1.34646 1.35176 
16 2017-09-19 1.35176 1.35515 1.34683 1.35178 
17 2017-09-20 1.35178 1.36573 1.34517 1.34914 
18 2017-09-21 1.34914 1.35861 1.34706 1.35761 
19 2017-09-22 1.35760 1.35955 1.34496 1.34935 
20 2017-09-24 1.34872 1.35339 1.34832 1.35261 
21 2017-09-25 1.35262 1.35706 1.34308 1.34816 
22 2017-09-26 1.34816 1.35139 1.34094 1.34385 
23 2017-09-27 1.34386 1.34389 1.33634 1.33753 
24 2017-09-28 1.33754 1.34551 1.33427 1.34159 
25 2017-09-29 1.34160 1.34258 1.33497 1.33963 
+0

Was haben Sie bisher versucht? Verwenden Sie 'dput', um Ihre Daten anzuzeigen. – jsb

Antwort

1

versuchen Convert date-time format for use in xts für weitere Informationen, aber das funktioniert auf einer Teilmenge der Daten.

library(xts) 
df = read.table(file = "Your table here", header = TRUE) 

df$[row_counts] = NULL ## Remove if row counts are imported in the first column (i.e. the 1 2 3 4....) 

## Save the date in a separate identifier as character 
dates = as.character(df$Date) 

## Remove date values from table 
df$Date = NULL 

## Create xts object with hyphens to account for your format 
my_xts_object = xts(df, as.POSIXct(dates, format="%Y-%m-%d")) 
+0

Ich stimme der Spalte V1 zu. Auch in den aktuellen Daten macht das na.omit in diesem Dataset nichts und wird im Edit entfernt. Das Objekt wird im my_xts_object gespeichert. –

1
my.dat <- read.table(text= "Date Open High  Low Close 
1 2017-09-01 1.29360 1.29951 1.29052 1.29495 
2 2017-09-03 1.29600 1.29660 1.29444 1.29535 
3 2017-09-04 1.29536 1.29643 1.29124 1.29300 
4 2017-09-05 1.29306 1.30440 1.29088 1.30331 
5 2017-09-06 1.30331 1.30820 1.30181 1.30471 
6 2017-09-07 1.30472 1.31381 1.30325 1.31319 
7 2017-09-08 1.31319 1.32239 1.31233 1.31945 
8 2017-09-10 1.31816 1.31978 1.31736 1.31769 
9 2017-09-11 1.31769 1.32223 1.31593 1.31764 
10 2017-09-12 1.31766 1.33145 1.31703 1.33097 
11 2017-09-13 1.33096 1.33286 1.31836 1.32040 
12 2017-09-14 1.32039 1.34055 1.31501 1.33955 
13 2017-09-15 1.33955 1.36159 1.33922 1.35871 
14 2017-09-17 1.35579 1.36029 1.35579 1.35921 
15 2017-09-18 1.35921 1.36181 1.34646 1.35176 
16 2017-09-19 1.35176 1.35515 1.34683 1.35178 
17 2017-09-20 1.35178 1.36573 1.34517 1.34914 
18 2017-09-21 1.34914 1.35861 1.34706 1.35761 
19 2017-09-22 1.35760 1.35955 1.34496 1.34935 
20 2017-09-24 1.34872 1.35339 1.34832 1.35261 
21 2017-09-25 1.35262 1.35706 1.34308 1.34816 
22 2017-09-26 1.34816 1.35139 1.34094 1.34385 
23 2017-09-27 1.34386 1.34389 1.33634 1.33753 
24 2017-09-28 1.33754 1.34551 1.33427 1.34159 
25 2017-09-29 1.34160 1.34258 1.33497 1.33963", 
header=TRUE, colClasses=c("integer","Date", rep("numeric", 4))) 

Dann XTS Klasse umwandeln, die eine Matrix und ein Index ist, so dass Sie die Datumsspalte von dem Objekt als x weitergegeben entfernen müssen.

my.xts <- as.xts(my.dat[-1], order.by=my.dat[[1]]) 
str(my.xts) 
#----------- 
An ‘xts’ object on 2017-09-01/2017-09-29 containing: 
    Data: num [1:25, 1:4] 1.29 1.3 1.3 1.29 1.3 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : NULL 
    ..$ : chr [1:4] "Open" "High" "Low" "Close" 
    Indexed by objects of class: [Date] TZ: UTC 
    xts Attributes: 
NULL 
Verwandte Themen