From hosking at watson.ibm.com  Fri Apr 16 18:31:33 2004
From: hosking at watson.ibm.com (hosking@watson.ibm.com)
Date: Fri Apr 16 18:31:37 2004
Subject: [Rd] type.convert (PR#6781)
Message-ID: <20040416163133.1085710495@slim.kubism.ku.dk>

Full_Name: J. R. M. Hosking
Version: 1.9.0
OS: Windows 2000
Submission from: (NULL) (129.34.20.23)


Two problems, perhaps related:

(1) na.strings is not honored when x is non-numeric and as.is=T

  > type.convert( c("abc","-"), as.is=T, na.strings="-" )
  [1] "abc" "-"  

... unless x consists only of NAs

  > type.convert( c("abc","-"), as.is=T, na.strings=c("-","abc") )
  [1] NA NA

But with x numeric or as.is FALSE (or omitted), it works as advertised:

  > type.convert( c("abc","-"), na.strings="-" )
  [1] abc  <NA>
  Levels: abc
  > type.convert( c("6","-"), na.strings="-" )
  [1]  6 NA


(2) When na.strings is omitted, blank strings in nonnumeric vectors are not
converted into NAs (regardless of the value of as.is).

  > type.convert(c("6",""," "))     # OK: gives 6 NA NA
  [1]  6 NA NA

  > type.convert(c("A",""," "))   # gives a factor with 3 levels and no NAs
  [1] A    
  Levels:    A

  > type.convert(c("A",""," "),as.is=T)  # gives a char vector with no NAs
  [1] "A" ""  " "


Rider: it would be nice if type.convert had a strip.white argument, so that
  type.convert(c(" 6"," -"),na.strings="-",strip.white=T) 
would return a numeric vector. Stripping leading and trailing blanks can be
time-consuming, and could presumably be done more quickly by an .Internal
function such as the one called by type.convert.


(R 1.9.0, Windows binary from CRAN)


[Rd] type.convert (PR#6781)
hosking@watson.ibm.com