-1

Possible Duplicate:
Replacing character values with NA in a data frame

I am reading a txt file with character and numeric values in each rows. Some of the missing values have been coded as xxxxxx in the file. I want to replace them by na in the data frame. Given below is a soecific row of the data frame i read in:

mydf[199,]
    season  size  speed    mxPH    mnO2      C1     NO3     NH4    oPO4     PO4
199 winter large medium 8.00000 7.60000 XXXXXXX XXXXXXX XXXXXXX XXXXXXX XXXXXXX
      Chla1 a1   a2  a3 a4 a5 a6  a7
199 XXXXXXX  0 12.5 3.7  1  0  0 4.9

I cant seem to replace the x's with na. Any help is greatly apprecisted!!!

Community
  • 1
  • 1
user597551
  • 979
  • 2
  • 9
  • 9
  • 2
    This question has already been answered. See http://stackoverflow.com/questions/3357743/replacing-character-values-with-na-in-a-data-frame – csgillespie Jul 01 '11 at 15:52

1 Answers1

1

The simplest approach is probably to set xxxxxxx to missing while reading the data from the file using the na.strings option. Here is a quick example using a text connection instead of a file:

 txt <- textConnection("1 3 Hi xxx\n 2 5 Low No")
 read.table(txt, na.string="xxx")
  V1 V2  V3   V4
1  1  3  Hi <NA>
2  2  5 Low   No
Aniko
  • 18,516
  • 4
  • 48
  • 45