I would argue this is as bad as Excel converting gene names to dates.
I assume there is a better alternative to read.table() without these quirks?
Hi,
I have a data set which includes A, T,C,G. When I use read.table command for this data set, I see TRUE's in terms of T alleles. It happens if the entire column just includes T. How can I fix that problem?
Thanks.
2 answers
read.table() tries to guess the class of the input data and will sometimes be mislead.
> read.table(text=("A C G T"))
V1 V2 V3 V4
1 A C G TRUE
> summary(read.table(text=("A C G T")))
V1 V2 V3 V4
A:1 C:1 G:1 Mode:logical
TRUE:1
NA's:0
It is possible to specify in advance the class of the columns; see ?read.table for details.
> read.table(text=("A C G T"), colClasses = "character")
V1 V2 V3 V4
1 A C G T
Related to Chris's answer in this particular case, stringAsFactors will not solve the problem.
> read.table(text=("A C G T"), stringsAsFactors = FALSE)
V1 V2 V3 V4
1 A C G TRUE
Note that there are other cases where T may be coerced to TRUE instead of "T". In particular, pay attention that there is one gene whose symbol is _T_ !
Not that I know,
> system("printf 'A C T G\nA C T G\n' > test.txt")
> read.table("test.txt")
V1 V2 V3 V4
1 A C TRUE G
2 A C TRUE G
> data.table::fread("test.txt", head = F)
V1 V2 V3 V4
1: A C TRUE G
2: A C TRUE G
> as.data.frame(readr::read_delim("test.txt", " ", col_names=FALSE))
Parsed with column specification:
cols(
X1 = col_character(),
X2 = col_character(),
X3 = col_logical(),
X4 = col_character()
)
X1 X2 X3 X4
1 A C TRUE G
2 A C TRUE G
However, this only happens when a column only contains values that look like logical.
> read.table(text="A C T G\nA C T G\n")
V1 V2 V3 V4
1 A C TRUE G
2 A C TRUE G
> read.table(text="A C T G\nA C F G\n")
V1 V2 V3 V4
1 A C TRUE G
2 A C FALSE G
> read.table(text="A C T G\nA C G G\n")
V1 V2 V3 V4
1 A C T G
2 A C G G
I suspect that using "read.table( . . . stringAsFactors=F)" will solve your problem.
(edit - it will not! see the comprehensive answer above)
Log in to answer this question.