[R] re placing <NA> in character column

Mark W. Miller mark_wayne_miller at yahoo.com
Wed Oct 28 23:22:59 CET 2009





I am guessing that your <NA> is not really an NA_character,  but  
rather a factor with a level of "<NA>".

See if str(s1) confirms my suspicions.

 > df1 <- read.table(textConnection(" Firstname  Lastname Age
+ 1    Bob       Smith   20
+ 2   John       Clark   NA
+ 3   Andy       <NA>    40"), header=T)
 > levels(df1$Lastname)
[1] "<NA>"  "Clark" "Smith"
 > levels(df1$Lastname)[1] <- "qqqq"
 > levels(df1$Lastname)
[1] "qqqq"  "Clark" "Smith"
 > df1
   Firstname Lastname Age
1       Bob    Smith  20
2      John    Clark  NA
3      Andy     qqqq  40

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT





Dr. Winsemius, I just tried your suggestion of typing str(s1).  Below is the
code and the comments that R returned:

> channel <- odbcConnectExcel('u:/test.xls')
> sqlTables(channel)

  TABLE_CAT TABLE_SCHEM TABLE_NAME   TABLE_TYPE REMARKS
1  u:\\test        <NA>    Sheet1$ SYSTEM TABLE    <NA>
2  u:\\test        <NA>    Sheet2$ SYSTEM TABLE    <NA>
3  u:\\test        <NA>    Sheet3$ SYSTEM TABLE    <NA>

> 
> s1 <- sqlFetch(channel, "Sheet1")
> 
> odbcClose(channel) 
> 
> s1

  Firstname Lastname Age
1       Bob    Smith  20
2      John    Clark  NA
3      Andy     <NA>  40


> str(s1)

'data.frame':   3 obs. of  3 variables:
 $ Firstname: Factor w/ 3 levels "Andy","Bob","John": 2 3 1
 $ Lastname : Factor w/ 2 levels "Clark","Smith": 2 1 NA
 $ Age      : num  20 NA 40

> levels(s1$Lastname)

[1] "Clark" "Smith"

> 



When I used the code:

levels(s1$Lastname)[1] <- "qqqq"

R replaced Clark with qqqq and kept <NA> I guess because <NA> is not
considered a level here:


> levels(s1$Lastname)[1] <- "qqqq"
> s1
  Firstname Lastname Age
1       Bob    Smith  20
2      John     qqqq  NA
3      Andy     <NA>  40
> 




-- 
View this message in context: http://www.nabble.com/replacing-%3CNA%3E-in-character-column-tp26102360p26103240.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list