0

I need to generate .dat files with some requirements, to be analysed by a specific software. The format of the .dat file should: have certain fixed width in the ID column, and show the item response values all together with no separators. Exactly like this:

(ID)   (i1i2i3...values) 
1      1234
2      1234
3      1234
4      1234
...
2365   1234

I could get the fixed width characters by IDfixed <- sprintf("%04d", ID), and include the variable in this code:

  for(i in 0:5) {
  for (j in 1:5) {
    filename <- paste("Factor",j, "_", "Facet",i+1, "_", "ID", ".dat", sep="")
    write.table(cbind(IDfixed, data[,c(2*i+1,2*i+2),j]), filename, col.names=F, row.names=F)
  }
}

The output shoulnd't have these "" characters -it must have the same format as the former table. A sample of the result:

     [,1]   [,2] [,3]
[1,] "0001" "2"  "1" 
[2,] "0002" "2"  "3" 
[3,] "0003" "5"  "5" 
[4,] "0004" "3"  "3" 
[5,] "0005" "3"  "5"

Thanks for your time!

Gina Zetkin
  • 333
  • 1
  • 5
  • 12

1 Answers1

0

Try this:

# sample data
ID <- c(1, 11, 111, 1111)

# convert ID to character, pad with leading zeros to a fixed width of 4
IDfixed <- sprintf("%04d", ID)

IDfixed
# [1] "0001" "0011" "0111" "1111"

edit The default behavior of write.table is that "any character or factor columns will be surrounded by double quotes" (quote = TRUE). To avoid the "" around elements in 'ID' to be written to the file, use quote = FALSE.

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Thanks for the idea, it has solved the width of the columns, but the output doesn't still meet the requirements, it looks like this: `"1029" "2" "5"`and there shouldn't be any "" and the item values should be together, with no spaces in between. – Gina Zetkin Sep 11 '13 at 07:21
  • I think you need to clarify what you are trying to do and exactly what is your requirements. It is impossible to guess what "the output" is. Please post some example data, the desired output and the code you have tried. Please read [this](http://meta.stackoverflow.com/help/how-to-ask), [this](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist) and [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Thanks! – Henrik Sep 11 '13 at 07:30
  • Sorry for the lack of information. I need to produce several .dat files to be analysed with a specific software. The format of the sheets must be like this: ` 1 1234 2 1234 3 1234 4 1234 ... 2365 1234` what means that the width of the ID cols should be fixed (a certain number of characters identical for any row), and that after blank spaces there must be the responses to items altogether – Gina Zetkin Sep 11 '13 at 08:08
  • Thanks for the additional information. I think it is best if you edit your actual question. Updates like this tends to 'disappear' if you only provide it in the comments. Again, a minimal reproducible starting data set, the code you have tried, and your desired output would help a lot. Cheers. – Henrik Sep 11 '13 at 11:45
  • Please read the `quote` argument in `write.table`. E.g. `df <- data.frame(x = letters[1:5], y = LETTERS[1:5]); df; write.table(x = df, file = "foo.txt", quote = FALSE)`. -> No quotes in foo.txt. – Henrik Sep 11 '13 at 15:45