3

I have a dataset that is stored into a variable called "new" and I am trying to write this to a txt file with fixed specified widths between each column.

I have already tried

library(gdata)

write.fwf(new, file = "test.txt", width = c(57,20,10,6,19,12,10,18), colnames = FALSE, sep = "")

but it seems as though the order that I put the width amounts in don't align with the corresponding columns, such as if I change some to a lower width, it gives "width was too small for column ColumnC" message, even though the width that I specified was for a columnB for example.

I am wanting to be able to set a specified width between each column to have a specific amount of spaces between each of them.

Below is the same txt file format, I just changed all of the names:

SPRN55 TEST ST                                            LOS ANGELES         CA 11111 DOE                 JOHN          1112223333           5555555510.00                N                                                  1111111111     
SPRN8658 TEST DRIVE                                       LOS ANGELES         CA 11111 DOE                 JOHN          1112223333           5555555510.00                N                                                  1111111111     
SPRN787 TEST DRIVE                                        LOS ANGELES         CA 11111 DOE                 JOHN          1112223333           5555555510.00                N                                                  1111111111     

The columns are in the order of ADDR, City, State, ZIP, Last name, first name, phone, account, amount, N, Number

Now when I tried the below code on this sample,

write.fwf(mydata, file = "test1.txt", width = c(57,20,3), colnames = FALSE, sep = "")

I get "'width' (57) was too small for columns: V1 'width' should be at least (237)"

This is similar issue to when I am running this against my real data.

zx8754
  • 52,746
  • 12
  • 114
  • 209
jwalls91
  • 341
  • 1
  • 5
  • 14
  • 1
    (a) Can you please mention which package your are using for `write.fwf`? Is it `gdata`, or something else? (b) Can you make a short reproducible example of your problem? Like, if you try on just the first 2 rows of `new`, do you see the issue? If so, please share the first 2 rows of `new` in a copy/pasteable way, like `dput(droplevels(new[1:2, ]))`. If those 2 rows *don't* illustrate the problem, them please find a subset that does and share that. – Gregor Thomas May 14 '19 at 13:41
  • a) Yes it is gdata package b) If I just specify the width on the first 2 columns, it works but gives warning message saying "recycling width" and "number of items to replace is not a multiple of replacement length" – jwalls91 May 14 '19 at 13:55
  • I have edited my main post with an example. – jwalls91 May 14 '19 at 14:05
  • 1
    Can you please share data **that reproduces the problem**? And it would be very nice if you would **use `dput()` to share data** so it is copy-pasteable. When I read in the data you put in the question `df = read.table(text = 'ADDR CITY STATE Test_St Test_City CA', header = T)` (adding `_` so it parses... since you didn't use `dput`. Who knows about your column classes. It would be great if you would use `dput()` so we don't have to guess on that. Really, please use `dput()`). – Gregor Thomas May 14 '19 at 14:12
  • Then I run your command on that data, I get no warnings, no errors, and the output looks fine. So your sample data doesn't reproduce the problem. Can you find a subset of data that demonstrates the problem? Maybe your `ColumnE` is wider than you think... have a look at `sort(nchar(you_data$ColumnE), decreasing = T)`. But if you can't *show* us the problem, we have nothing to test possible solutions on. – Gregor Thomas May 14 '19 at 14:14
  • The number of characters (from `nchar()`) in each line of your example is 232. This seems remarkably close to the 237 in your error message, suggesting that the data are not actually in separate fields or variables. The advice of @Gregor is very wise! – David O May 14 '19 at 19:22

0 Answers0