[R] read.table and NaN

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Oct 24 06:08:34 CEST 2019


Like this?

con <- textConnection(object = 'A,B\n1,NaN\nNA,2')
> tmp <- read.table(con, header = TRUE, sep = ',', na.strings = '',
stringsAsFactors = FALSE,
+                   colClasses = c("numeric", "character"))
> close.connection(con)
> tmp
   A   B
1  1 NaN
2 NA   2
> class(tmp[,1])
[1] "numeric"
> class(tmp[,2])
[1] "character"
> tmp[,2]
[1] "NaN" "2"


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Oct 23, 2019 at 6:31 PM Sebastien Bihorel via R-help <
r-help using r-project.org> wrote:

> Hi,
>
> Is there a way to make read.table consider NaN as a string of characters
> rather than the internal NaN? Changing the na.strings argument does not
> seems to have any effect on how R interprets the NaN string (while is does
> not the the NA string)
>
> con <- textConnection(object = 'A,B\n1,NaN\nNA,2')
> tmp <- read.table(con, header = TRUE, sep = ',', na.strings = '',
> stringsAsFactors = FALSE)
> close.connection(con)
> tmp
> class(tmp[,1])
> class(tmp[,2])
>
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list