1

我有一个 csv 文件,其中的数据如下所示:

"Date","Time","Open","High","Low","Close","Up","Down","Sell","Buy","Stop1","Stop2","Stop3","Target1","Target2","Target3"
04/18/2012,1335,1365.25,1366.00,1365.00,1366.00,2855,1153,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00
04/18/2012,1340,1366.00,1366.25,1365.50,1366.00,450,607,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00
04/18/2012,1345,1366.00,1366.00,1365.75,1365.75,300,263,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00

如果我使用此命令读取文件,则会得到一个字符矩阵:

RawData = as.matrix(read.table(PathName,sep=",",header=TRUE))

typeof(RawData)
[1] "character"

最好我能说这是由于日期列。

我需要这是一个数字数组。我在上面使用的 read.table 命令有什么好的替代方法?

4

1 回答 1

4
RawData.df <- read.table(PathName,sep=",",header=TRUE)
RawData <- as.matrix(RawData.df[,-1])

在您的数据上:

> RawData.df <- read.table(sep = ",", header = TRUE, text =
+ '"Date","Time","Open","High","Low","Close","Up","Down","Sell","Buy","Stop1","Stop2","Stop3","Target1","Target2","Target3"
+ 04/18/2012,1335,1365.25,1366.00,1365.00,1366.00,2855,1153,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00
+ 04/18/2012,1340,1366.00,1366.25,1365.50,1366.00,450,607,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00
+ 04/18/2012,1345,1366.00,1366.00,1365.75,1365.75,300,263,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00')
> str(RawData.df)
'data.frame':   3 obs. of  16 variables:
 $ Date   : Factor w/ 1 level "04/18/2012": 1 1 1
 $ Time   : int  1335 1340 1345
 $ Open   : num  1365 1366 1366
 $ High   : num  1366 1366 1366
 $ Low    : num  1365 1366 1366
 $ Close  : num  1366 1366 1366
 $ Up     : int  2855 450 300
 $ Down   : int  1153 607 263
 $ Sell   : num  0 0 0
 $ Buy    : num  0 0 0
 $ Stop1  : num  0 0 0
 $ Stop2  : num  0 0 0
 $ Stop3  : num  0 0 0
 $ Target1: num  0 0 0
 $ Target2: num  0 0 0
 $ Target3: num  0 0 0
> RawData <- as.matrix(RawData.df[,-1])
> str(RawData)
 num [1:3, 1:15] 1335 1340 1345 1365 1366 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:15] "Time" "Open" "High" "Low" ...
于 2012-10-30T19:37:48.397 回答