[R] replacing <NA> in character column
David Winsemius
dwinsemius at comcast.net
Wed Oct 28 22:49:46 CET 2009
On Oct 28, 2009, at 5:19 PM, Mark Miller wrote:
> I have a small Excel data file with two columns of character
> variables, one column with a numeric variable and three rows. One
> of the character cells is blank and one of the numeric cells is blank.
>
> I read the data file with the following code:
>
>
> library(RODBC)
>
> channel <- odbcConnectExcel('u:/test.xls')
> sqlTables(channel)
>
> s1 <- sqlFetch(channel, "Sheet1")
>
> odbcClose(channel)
>
> s1
>
>
>
> The code prints the data file as:
>
> Firstname Lastname Age
> 1 Bob Smith 20
> 2 John Clark NA
> 3 Andy <NA> 40
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
>
>
> I can replace the NA with the number zero in the second row of the
> third (‘Age’) column using the following line:
>
> s1[is.na(s1[,3]), 3] <- 0
>
>
> What code can I use to replace the <NA> in the third row of the
> second (‘Lastname’) column with qqqqqqqq?
>
> Thank you in advance for any advice.
>
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list