[R] fill=T?

jim holtman jholtman at gmail.com
Thu Mar 8 02:30:35 CET 2012


If it messes up your data, then it indicates that your data is messed
up to start with.  Go to the source of you data and see if they can
put field separators.  If not, then if the data is column-wise fixed
format, then use read.fwf to read in the data.  If the data is as your
mail shows it, then R will read in 4 value and put them in the first 4
columns.  If the next row only has two values, it will put them in the
first 2 columns.

Here is read.fwf

> x <- read.fwf(textConnection("> a    b    c    d
+ > 1    2    3    4
+ > 1         8    6
+ > 1    2         0
+ > 1    1    0"), width = c(-2,5,5,5,1))
> closeAllConnections()
> x
     V1    V2    V3   V4
1 a     b     c        d
2 1     2     3        4
3 1           8        6
4 1     2              0
5 1     1         0 <NA>
>


On Wed, Mar 7, 2012 at 4:32 PM, karena <dr.jzhou at gmail.com> wrote:
> If a text file has rows of variable lengths. How to read the file into R?
>
> I think some people may suggest using 'fill=T', however, it sort of messes
> the data up, for example, in the text file:
> a    b    c    d
> 1    2    3    4
> 1          8    6
> 1    2          0
> 1    1    0
>
> If I read in the file using 'read.table("data", head=T, fill=T), then the
> data.frame in R will be:
> a    b    c    d
> 1    2    3    4
> 1    8    6    NA
> 1    2    0    NA
> 1    1    0    NA
>
> Could anyone tell me how to handle this?
>
> Thank you very much,
>
> Karena
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/fill-T-tp4454751p4454751.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list