1

I want to read a ASCII file to R environment. However, the ASCII file is a non delimited and the data is not continuous (have some blank spaces between the variables) so in order to read the data i have used the below syntax i.e

test <- read.fwf("D:/R_process/ASCII.txt", width = c(10, 4, 1, 4, 9, 9, 1,1,1,1,1,1,1,3,8))

Now I am able to read it but the data read is wrong. Actually, my out put should have only the applicable variables data but not the blank data. Below is the ASCII data. Please let me know how should i write the syntax to read only the applicable data in the file.

Thanks for your help in advance.

Here is the Data:

000000000120151  04 0.766696           1                         1000000000 010

000000000220151  04 1.458186           1                         1000100000 020

000000000320151  04 0.185492           1                         1000000000 015

000000000420151  04 0.961584           1                         1000000000 003

000000000520151  04 0.650091           2                         0001000000    

000000000620151  04 0.430350           1                         1000000000 020

000000000720151  04 3.192895           2                         1011000000 000

000000000820151  04 0.617127           1                         1010100000 015

000000000920151  04 0.399207           1                         1000000000 010
3
  • Probably i was not clear in my earlier explanation i hope so...In general if i am reading to SPSS environment i use the variable start and end position of the columns to read the particular variable so i SPSS code would be SERIAL 1-10 YEAR 11-14 WAVE 15 CNTR 16-19 WEI 20-28 SMK 40 Brand1 71 Brand1 72 Brand1 73 Brand1 74 Brand1 75 Brand1 80 ABC 82-84 Main 101-108.. now i want to read the same to R i.e. only the applicable column/variable data no need of the blank data.
    – Jagadish
    Jul 10, 2015 at 13:38
  • I thought my answer would read the data ignoring the blank data, doesn't it? Could you provide an example output? Jul 10, 2015 at 13:50
  • @Jagadish does strip.white not do what you want? You could also use negative numbers in the width to skip columns. Jul 10, 2015 at 21:35

2 Answers 2

1

You can use the strip.white parameter on read.fwf.

test <- read.fwf("D:/R_process/ASCII.txt",
  width = c(10, 4, 1, 4, 9, 9, rep(1, 8), 3, 8),
  strip.white = TRUE)
1
read.table("D:/R_process/ASCII.txt", sep = " ")

EDIT After @Nick K comment below, new answer:

I understand you just have the wrong widths:

read.fwf("D:/R_process/ASCII.txt", width = c(10, 4, 1, 4, 9, 12, 35, 4))  
1
  • This won't work for the OP's file - the numbers in the 4th column are actually to be read individually. Jul 10, 2015 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.