2

I have several ASCII files I Need to Import into R with return data for different asset classes. The structure of the ASCII files is as follows (with 2 sample data)

How can I Import this? I wasn't succesfull with read.table, but I would like to have it in a data.frame Format.

<Security Name> <Ticker> <Per> <Date> <Close>
Test Description,Test,D,19700101,1.0000
Test Description,Test,D,19700102,1.5
4

2 回答 2

5

如果你真的想强制列名进入 R,你可以使用类似的东西:

# Data
dat <- read.csv("/path/to/data.dat", header = FALSE, skip = 1)
dat
                V1   V2 V3       V4  V5
1 Test Description Test  D 19700101 1.0
2 Test Description Test  D 19700102 1.5


# Column names
dat.names <- readLines("/path/to/data.dat", n = 1)
names(dat) <- unlist(strsplit(gsub(">", " ", gsub("<", "", dat.names)), "  "))
dat
     Security Name Ticker Per     Date Close 
1 Test Description   Test   D 19700101    1.0
2 Test Description   Test   D 19700102    1.5

虽然我认为可能有更好的解决方案,例如手动添加标题......

于 2013-06-17T14:38:56.103 回答
1

您可以使用 read.csv 轻松读取此数据。由于您的列名不是逗号分隔的,因此您需要使用 header=FALSE 参数,然后在数据位于 R 中后添加名称,或者 oyu 可以在读取数据之前手动编辑数据,方法是省略 <> 字符并添加逗号在每个列名之间。

于 2013-06-17T14:31:01.230 回答