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
MichiZH
  • 5,587
  • 12
  • 41
  • 81

2 Answers2

5

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

fdetsch
  • 5,239
  • 3
  • 30
  • 58
1

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.

Jeffrey Evans
  • 2,325
  • 12
  • 18
  • I've used RA2<-read.table("O:/Daten/ASCII/ARiskInd/RA2_AA_CLASS2_CHF_A.asc", sep=",", header=TRUE) However I always get the error: "Error in read.table(...) more columns than column names. But I have it exactly like posted above, so there are exactly the same number of column names as there are columns... – MichiZH Jun 17 '13 at 14:36
  • 1
    @MichiZH, there are no commas in the line containing variable names, so why would you expect them to work? I would suggest reading in the file skipping the first line (and `header = FALSE`) and then manually adding the headers back in. – A5C1D2H2I1M1N2O1R2T1 Jun 17 '13 at 14:39
  • Or maybe the error is that there are some whitespaces between the last comma and the 1.0000. But with sep="," this shouldn't be an issue right? I have to work with the data as they are (with the whitespaces) because there are hundreds of ASCII files and they work in other programs as they are... – MichiZH Jun 17 '13 at 14:39
  • @MichiZH, no. That whitespace should not cause any problems. – A5C1D2H2I1M1N2O1R2T1 Jun 17 '13 at 14:41
  • The skip was the missing part. Thanks a lot! how could I include the Headers back in automatically? Like read only the first line (sep = " " now) and skip the rest? What would the code look like? – MichiZH Jun 17 '13 at 14:42