Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read.table function in R cannot read 'i'

Tags:

r

read.table or read.csv functions will return NA is "i" in the cells of csv file. For example

read.table(text <- textConnection('a, b, c
b,i,i
'), sep = ',', head = TRUE)


a  b  c
1 b NA NA

Any suggestions to solve this problem?

> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.3 formatR_0.8    tools_3.0.1  

Cheers, Bangyou

like image 927
Bangyou Avatar asked Feb 13 '26 09:02

Bangyou


1 Answers

This arises because you have not specified the classes for your columns using colClasses.

As such read.table uses type.convert attempts to convert it to logical, integer, numeric or complex.

i is interpreted by type.convert as attempting to be complex, but returns NA.

type.convert('i')
## NA

Use `colClasses = 'character') to force all columns to be character, or use a named vector to ensure that you set the classes for the columns you wish.

like image 198
mnel Avatar answered Feb 15 '26 01:02

mnel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!