Hi all, In some circumstances, as.character applied to a list converts real NA's into the string "NA". Propagation of NAs is something R does very well and unless there are good reasons for losing the NA, it would improve the consistency w.r.t. NA handling for as.character to behave differently. Here's an example: ## Create a list with character, logical, and integer NA elements v <- list(a=as.character(NA), b=NA, c=as.integer(NA)) sapply(v, is.na) a b c TRUE TRUE TRUE sapply(as.character(v), is.na) <NA> NA NA TRUE FALSE FALSE Thoughts? + seth -- Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center http://bioconductor.org
Seth Falcon <sfalcon at fhcrc.org> writes:> Hi all, > > In some circumstances, as.character applied to a list converts real > NA's into the string "NA". Propagation of NAs is something R does > very well and unless there are good reasons for losing the NA, it > would improve the consistency w.r.t. NA handling for as.character to > behave differently. > > Here's an example: > > ## Create a list with character, logical, and integer NA elements > v <- list(a=as.character(NA), b=NA, c=as.integer(NA)) > sapply(v, is.na) > > a b c > TRUE TRUE TRUE > > sapply(as.character(v), is.na) > > <NA> NA NA > TRUE FALSE FALSE > > Thoughts?Hmm...> as.character(v)[1] NA "NA" "NA" This does look like a leftover from times when there was no character NA in the language. It is the kind of thing you need to be very careful about fixing though. (I have a couple of scars from as.character on formulas when introducing backtick quoting.) BTW, another little bit of nastiness popped up when playing around with this:> dput(v,control="all")structure(list(a = NA, b = NA, c = as.integer(NA)), .Names = c("a", "b", "c"))> sapply(v,mode)a b c "character" "logical" "numeric" -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Also, Splus 7 has a different behaviour from R: > sapply(v, is.na) a b c F T T > sapply(as.character(v), is.na) [1] F F F > as.character(v) [1] "\"NA\"" "NA" "NA" In R, it remains logical: if it isn't in character mode, then as.character always turns NA's into "NA"'s. If that behaviour is not desired, and you want to keep character NA's as true NA's, then you can test from the original object. I agree that is dangerous to change the behaviour of simple things like this, and don't recommend any change in behaviour. However, this isn't documented, so here is my suggested patch for ?as.character . (Edit this as necessary). +mt
Okay ... I'll try to attach that patch once more ... (does this list only accept certain exertions for attachments? I used '.patch', but it must have been filtered off, so I'll try '.patch.txt' now ...) +mt -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: character.patch.txt Url: https://stat.ethz.ch/pipermail/r-devel/attachments/20061025/ff0cc2f5/attachment-0004.txt