Import ASCII file into R

前端 未结 2 497
执念已碎
执念已碎 2021-01-25 02:42

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

相关标签:
2条回答
  • 2021-01-25 03:06

    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...

    0 讨论(0)
  • 2021-01-25 03:11

    You can easily read this data using read.csv. Since your column names are not comma separated then you will need to use the header=FALSE argument and then add the names once the data is in R or oyu can manually edit the data before reading it by omitting the <> characters and adding a comma between each column name.

    0 讨论(0)
提交回复
热议问题