Read.table unexpectedly interprets "T" as TRUE

I found a problem in which R seems to interpret "T"how TRUEeven using all means to not do this (at least according to this post).

Sample data (stored as "test.txt"):

col1    col2
1   T
2   T
3   T
4   T
5   T
6   T
7   T
8   T
9   T

Code example:

read.table("test.txt", as.is=TRUE, header=TRUE, 
   stringsAsFactors=FALSE, colClasses=c(character())) 

It produces:

  col1 col2
1    1 TRUE
2    2 TRUE
3    3 TRUE
4    4 TRUE
5    5 TRUE
6    6 TRUE
7    7 TRUE
8    8 TRUE
9    9 TRUE

The only imperfect solution I found was to set header = FALSE:

read.table("test.txt", as.is=TRUE, header=FALSE, 
    stringsAsFactors=FALSE,
    colClasses=c(character()))        


     V1   V2
1  col1 col2
2     1    T
3     2    T
4     3    T
5     4    T
6     5    T
7     6    T
8     7    T
9     8    T
10    9    T

I understand that this may seem somewhat far-fetched, but this marginal case is genuine in that the human gene is actually named "T"(!) With values ​​in col1that are positions inside this gene.

Thanks in advance for your help

+4
source share
1 answer

, ""?

R ( ), , colClasses=..., R.

R> res <- read.table(textConnection("col1 col2\n1 T\n2 T\n3 T"), 
+                    header=TRUE, colClasses=c("numeric", "character"))
R> res
    col1 col2
 1    1    T 
 2    2    T 
 3    3    T 
R> sapply(res, class)
        col1        col2  
   "numeric" "character"  
R>

, , colClasses. , , .

+7

All Articles