Blank.lines.skip = TRUE with read.fwf error?

There are four blank lines at the end of my file.

> data=read.fwf("test2",head=F,widths=c(3,1,-3,4,-1,4),blank.lines.skip = TRUE)  
> data  

When I run this code, the blank.lines.skip argument is ignored. I still get blank lines in my output.

File:

x1     F          1890 1962  
x2                1857 1936  
x3                1900 1978  
x4                1902 1994  
x5        F       1878 1939 

and four blank lines at the end.

+1
source share
1 answer

It seems that you are right that it blank.lines.skipdoes not apply read.fwf- you have to dig into the code to find out why, but it read.fwfdoes significant processing of the file before transferring it (along with the directive blank.lines.skip) to read.table. However, it is not so difficult to detect and remove all spaces after the fact.

For instance:

cat("abc","def","ghi","","","",sep="\n",file="test3.dat")
read.table("test3.dat")  ## blank lines skipped (by default)
(x <- read.fwf("test3.dat",widths=c(1,1,1),blank.lines.skip=TRUE))
##     V1   V2   V3
## 1    a    b    c
## 2    d    e    f
## 3    g    h    i
## 4 <NA> <NA> <NA>
## 5 <NA> <NA> <NA>
## 6 <NA> <NA> <NA>
all_NA <- apply(x,1,function(z) all(is.na(z)))
x[!all_NA,]

( , , , , , , , ): colClasses "", , (a) .

+5

Source: https://habr.com/ru/post/1671371/


All Articles