fd=:'mytestfile.txt' NB. TAB text file
NB. a \t 1 \t 2 \t 3
NB. b \t \t 5 \t 6
NB. c \t 7 \t \t 9
NB. d \t 10 \t 11 \t 12
NB. \t 13 \t 14 \t 15

1    a  1  2  3
2    b NA  5  6
3    c  7 NA  9
4    d 10 11 12
5 <NA> 13 14 15


fd=: 0 : 0
NB. a \t 1 \t 2 \t 3
NB. b \t \t 5 \t 6
NB. c \t 7 \t \t 9
NB. d \t 10 \t 11 \t 12
NB. \t 13 \t 14 \t 15
)

   [fd2=:fd rplc '\t \t';'\t NA \t'
NB. a \t 1 \t 2 \t 3
NB. b \t NA \t 5 \t 6
NB. c \t 7 \t NA \t 9
NB. d \t 10 \t 11 \t 12
NB. \t 13 \t 14 \t 15

   [fd3=:fd2 rplc 'NB. \t';'NB. <NA> \t'
NB. a \t 1 \t 2 \t 3
NB. b \t NA \t 5 \t 6
NB. c \t 7 \t NA \t 9
NB. d \t 10 \t 11 \t 12
NB. <NA> \t 13 \t 14 \t 15

   [fd4=:fd3 rplc '\t';TAB
NB. a  1  2  3
NB. b  NA  5  6
NB. c  7  NA  9
NB. d  10  11  12
NB. <NA>  13  14  15

   [fd5=:fd4 rplc 'NB.';'x',TAB
x a  1  2  3
x b  NA  5  6
x c  7  NA  9
x d  10  11  12
x <NA>  13  14  15

   [fd6=:('12345')(I.'x' = fd5) } fd5
1 a  1  2  3
2 b  NA  5  6
3 c  7  NA  9
4 d  10  11  12
5 <NA>  13  14  15




   >cut &.> (<;._2) fd6
┌──┬────┬─┬──┬─┬──┬─┬──┐
│1 │a   │ │1 │ │2 │ │3 │
├──┼────┼─┼──┼─┼──┼─┼──┤
│2 │b   │ │NA│ │5 │ │6 │
├──┼────┼─┼──┼─┼──┼─┼──┤
│3 │c   │ │7 │ │NA│ │9 │
├──┼────┼─┼──┼─┼──┼─┼──┤
│4 │d   │ │10│ │11│ │12│
├──┼────┼─┼──┼─┼──┼─┼──┤
│5 │<NA>│ │13│ │14│ │15│
└──┴────┴─┴──┴─┴──┴─┴──┘

2012/11/7 kamakura <[email protected]>

> Hi
>
> I would like to know J's manipulation for reading text file.
>
> fd=:'mytestfile.txt' NB. TAB text file
> NB. a \t 1 \t 2 \t 3
> NB. b \t \t 5 \t 6
> NB. c \t 7 \t \t 9
> NB. d \t 10 \t 11 \t 12
> NB. \t 13 \t 14 \t 15
>
> fd2=:'mytestfile.csv' NB. CSV test file
>
> load'files misc'
>    freads fd2
> a,1,2,3
> b,,5,6
> c,7,,9
> d,10,11,12
> ,13,14,15
>
>
>    freadr fd ;0 1
> a       1       2       3
>
>    freadr fd ;1 1
> b               5       6
>
>    freadr fd ;2 1
>         7               9
> d
>    freadr fd ;3 1
> 10      11      1
>    freadr fd ;4 1
>
>         13      14
>
> The 3rd and 4th rows are read differently.  I expect that the following
> line will come out.
>
>    freadr fd ;2 1
> c       7               9
>
>
>
>    u=:'m' fread fd
>    u
> a       1       2       3
> b               5       6
> c       7               9
> d       10      11      12
>         13      14      15
>
>    TAB chop "1 u
> +--+--+-----+----+
> |a |1 |2    |3   |
> +--+--+-----+----+
> |b |5 |6    |    |
> +--+--+-----+----+
> |c |7 |     |9   |
> +--+--+-----+----+
> |d |10|11   |12  |
> +--+--+-----+----+
> |13|14|15   |    |
> +--+--+-----+----+
>
>
> How can I get the following table?
>
> +--+--+-----+----+
> |a |1 |2    |3   |
> +--+--+-----+----+
> |b |NA|5    |6   |
> +--+--+-----+----+
> |c |7 |9    |NA  |
> +--+--+-----+----+
> |d |10|11   |12  |
> +--+--+-----+----+
> |NA|13|14   |15  |
> +--+--+-----+----+
>
> R reads this text file as follows:
>
> > u=read.table("mytestfile.txt",header=F,na.strings="",sep="\t")
> > u
>     V1 V2 V3 V4
> 1    a  1  2  3
> 2    b NA  5  6
> 3    c  7 NA  9
> 4    d 10 11 12
> 5 <NA> 13 14 15
>
> Do you have any convenient utility function for reading text files?
>
>
>
> +++++++++++++++++++++++++++++
>     Toshinari Kamakura
>
>     Chuo University
>     1-13-27 Kasuga
>     Bunkyo-ku
>     Tokyo 112-8551, Japan
> ++++++++++++++++++++++++++++++
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>



-- 
Björn Helgason, Verkfræðingur
Fornustekkum II
781 Hornafirði,
t-póst: [email protected]
gsm: +3546985532
twitter: @flugfiskur
http://groups.google.com/group/J-Programming


Tæknikunnátta höndlar hið flókna, sköpunargáfa er meistari einfaldleikans

góður kennari getur stigið á tær án þess að glansinn fari af skónum
          /|_      .-----------------------------------.
         ,'  .\  /  | Með léttri lund verður        |
     ,--'    _,'   | Dagurinn í dag                     |
    /       /       | Enn betri en gærdagurinn  |
   (   -.  |        `-----------------------------------'
   |     ) |         (\_ _/)
  (`-.  '--.)       (='.'=)   ♖♘♗♕♔♙
   `. )----'        (")_(") ☃☠
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to