将ASCII文件导入R [英] Import ASCII file into R

查看:182
本文介绍了将ASCII文件导入R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个需要导入到R中的ASCII文件,其中包含不同资产类别的返回数据. ASCII文件的结构如下(带有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)

如何导入?我对read.table并不满意,但是我想以data.frame格式保存它.

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

推荐答案

如果您真的想将列名强制为R,则可以使用类似的方法:

If you really want to force the column names into R, you could use something like that:

# 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

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

Although I think there might be better solutions, e.g. manually adding the header...

这篇关于将ASCII文件导入R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆