[R] Reading a file with random whitespace

R. Michael Weylandt michael.weylandt at gmail.com
Thu Apr 19 22:39:28 CEST 2012


It looks like you might want to use read.fwf  for fixed width files

Michael

On Thu, Apr 19, 2012 at 3:45 PM, John S <john.smithr75 at gmail.com> wrote:
> Dear R-users,
>
> I have a large data file that I am trying to read in R where the file has a
> white space at the begging and at random places. The whitespace is actually
> NA so I am trying to read the file using scan but with no success!! Please
> help
>
> Here are some example rows from the data file.
>
>
>
>               1       1       0       0       2               1       1
>
>       0       0       2       1      21       0       2     180    64.5
>
>    19.9     0.8    54.5    1.86   133.3               0
>
>               1       1     0.6     0.6               0       0       0
>
>
>
>
>
>
>
>  Yes the whitespace is NA so with someone help it should look like
>
>
>
> NA       1       1          0          0         2       NA          1
> 1
>
>  0        0       2          1          21       0        2         180
> 64.5
>
> 19.9     0.8    54.5    1.86     133.3   NA      0        NA     NA
>
>
>
>
>
>
>
>
>
> Here is my code
>
> test
> <-scan(泥ATA",nlines=9,strip.white=F,blank.lines.skip=F,sep=",",what="")
>
> dput(test)
>
>
>
> c("               1       1       0       0       2               1       1",
>
>
> "       0       0       2       1      21       0       2     180    64.5",
>
> "    19.9     0.8    54.5    1.86   133.3               0        ",
>
> "               1       1     0.6     0.6               0       0       0",
>
> "                       2       1      21       0       2     180    64.5",
>
> "    19.9     0.8    54.5    1.86   133.3               0        ",
>
> "               1       1     1.1     1.1             3.2       0       0",
>
> "                       2       1      21       0       2     180    64.5",
>
> "    19.9     0.8    54.5    1.86   133.3               0        "
>
> )
>
>
>
> I tried to use na.string="[[:space:]]+$"  with no luck.
>
> Thanks,
>
> John
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


More information about the R-help mailing list