[R] Read

Bill Dunlap w||||@mwdun|@p @end|ng |rom gm@||@com
Mon Feb 22 23:39:04 CET 2021


Since the columns in the file are separated by a space character, " ",
add the read.table argument sep=" ".

-Bill

On Mon, Feb 22, 2021 at 2:21 PM Val <valkremk using gmail.com> wrote:
>
> Hi all, I am trying to read a messy data  but facing  difficulty.  The
> data has several columns separated by blank space(s).  Each column
> value may have different lengths across the rows.   The first
> row(header) has four columns. However, each row may not have the four
> column values.  For instance, the first data row has only the first
> two column values. The fourth data row has the first and last column
> values, the second and the third column values are missing for this
> row..  How do I read this data set correctly? Here is my sample data
> set, output and desired output.   To make it clear to each data point
> I have added the row and column numbers. I cannot use fixed width
> format reading because each row  may have different length for  a
> given column.
>
> dat<-read.table(text="x1  x2  x3 x4
> 1 B22
> 2         C33
> 322 B22      D34
> 4                 D44
> 51         D53
> 60 D62            ",header=T, fill=T,na.strings=c("","NA"))
>
> Output
>       x1  x2     x3     x4
> 1   1     B12 <NA> NA
> 2   2    C23 <NA>  NA
> 3 322  B32  D34   NA
> 4   4   D44  <NA>  NA
> 5  51 D53  <NA>   NA
> 6  60 D62  <NA>  NA
>
>
> Desired output
>    x1   x2     x3       x4
> 1   1    B22    <NA>   NA
> 2   2   <NA>  C33     NA
> 3 322  B32    NA      D34
> 4   4   <NA>   NA      D44
> 5  51  <NA>  D53     NA
> 6  60   D62   <NA>   NA
>
> Thank you,
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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