3

I want this c(002, 005/010) to become c("002", "005/010").

as.character(c(002, 005/010)) doesn't work, as it returns c("2", "0.5")

So I need to keep any leading zeros (including the 005 and 010 in the example), and I want to convert to character without first performing that calculation (perhaps I can first sub the / with ws...)

The actual values are from much longer text files of csvs, e.g.

047126002/047126003/047126004/047126005, 047016064, 047033008,
047265002, 047325004, 047265004, 047265003, 047033010,
047082004/047082005, 047058002, 047016030, 047320007, 047313005, ...
D L Dahly
  • 560
  • 3
  • 17
  • 1
    Your only chance to change `005/010` to something other than `0.5` is at the time of input. You can add leading 0s with `sprintf` or `formatC`, [as in this R-FAQ](https://stackoverflow.com/q/5812493/903061). But the denominator you used to make `0.5`, whether its `1/2`, `5/10`, or `005/010`, is not preserved. If these are inputs to a function you can grab them before they're evaluated... please edit your question if that is the case. – Gregor Thomas Nov 23 '17 at 19:46
  • The vectors are the output of another program, and can be very long. They are actually area identifiers, so carry no numeric info. If I read them is as numbers, the x/x calculations happen; if I read them in as character, the calc still happens, and the zeros are lost. – D L Dahly Nov 23 '17 at 20:01
  • "output of another program" is it a text file? – zx8754 Nov 23 '17 at 20:46
  • Yes, it can be a text file. – D L Dahly Nov 23 '17 at 20:48
  • Can you add example text file to your post? – zx8754 Nov 23 '17 at 20:55
  • 3
    Try: `df1 <- read.table("myFile.txt", sep = ",", fill = TRUE, colClasses = rep("character", 6))` – zx8754 Nov 23 '17 at 21:04
  • 3
    Or read.csv("myFile.csv", header = F, colClasses = rep("character", )) – You-leee Nov 23 '17 at 21:09
  • Those approaches work. Thanks. – D L Dahly Nov 26 '17 at 13:33

0 Answers0