Dear List I have a fixed width file with variables of varying width. The help is pretty transparent for this feature, but I can't seem to figure out how I can make effective use of the package with my data. In my dataset, the first 80 columns are of width 1 followed by other variables with width larger than 1. I think the correct way to do this, by brute force, would be> read.fwf(dataset, 1,1,1,...,1,5,...).I'm sure I do not need to actually enter 80 "1"s, correct? How might I do this more effectively? Harold [[alternative HTML version deleted]]
You can use rep() in specifying the widths argument to avoid typing all those ones. For example: read.fwf(file="c:\\mydata.dat", widths = c(rep(1,80), 5, 4, 6)) hope this helps, Chuck Doran, Harold wrote:> I have a fixed width file with variables of varying width. The help is > pretty transparent for this feature, but I can't seem to figure out how > I can make effective use of the package with my data. > > In my dataset, the first 80 columns are of width 1 followed by other > variables with width larger than 1. > > I think the correct way to do this, by brute force, would be > >>read.fwf(dataset, 1,1,1,...,1,5,...). > > I'm sure I do not need to actually enter 80 "1"s, correct? How might I > do this more effectively?-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Doran, Harold wrote:> Dear List > > I have a fixed width file with variables of varying width. The help is > pretty transparent for this feature, but I can't seem to figure out how > I can make effective use of the package with my data. > > In my dataset, the first 80 columns are of width 1 followed by other > variables with width larger than 1. > > I think the correct way to do this, by brute force, would be > > >>read.fwf(dataset, 1,1,1,...,1,5,...). > > > I'm sure I do not need to actually enter 80 "1"s, correct? How might I > do this more effectively??read.fwf: "widths integer vector, giving the widths of the fixed-width fields (of one line)." Hence: read.fwf(dataset, c(rep(1, 80), 5, ...)) Uwe Ligges> Harold > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
It might help to read that help file again:> d <- paste(c(rep(0, 80), 11111), collapse="") > d[1] "000000000000000000000000000000000000000000000000000000000000000000000000000 0000011111"> f <- file("try.dat", "w") > writeLines(d, f) > writeLines(d, f) > writeLines(d, f) > close(f) > fw <- c(rep(1, 80), 5) > x <- read.fwf("try.dat", fw) > xV1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38 V39 V40 V41 V42 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 V43 V44 V45 V46 V47 V48 V49 V50 V51 V52 V53 V54 V55 V56 V57 V58 V59 V60 V61 V62 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 V63 V64 V65 V66 V67 V68 V69 V70 V71 V72 V73 V74 V75 V76 V77 V78 V79 V80 V81 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11111 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11111 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11111 Andy> From: Doran, Harold > > Dear List > > I have a fixed width file with variables of varying width. The help is > pretty transparent for this feature, but I can't seem to > figure out how > I can make effective use of the package with my data. > > In my dataset, the first 80 columns are of width 1 followed by other > variables with width larger than 1. > > I think the correct way to do this, by brute force, would be > > > read.fwf(dataset, 1,1,1,...,1,5,...). > > I'm sure I do not need to actually enter 80 "1"s, correct? How might I > do this more effectively? > > Harold > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >