Trivia: R can have Comments in Tables

code
r
intermediate
tips
trivia
Author

Daniel Kick

Published

May 23, 2023

R allows for comments to exist in tables. If there’s a # in the table you’re reading (e.g. as a part of a column name like chromosome#) then it can cause an unequal number of values between rows (everything on that line following it is ignored). The solution is to specify the comment character explicitly to be used (it can be ’’ to have no comment characters). Here’s an example:

echo "a, b, c#, d" > test_table.txt
> Rscript -e "read.table('test_table.txt')"
#   V1 V2 V3
# 1 a, b,  c
> Rscript -e "read.table('test_table.txt', comment.char = '')" # with no comment character, all entries will be read
#   V1 V2  V3 V4
# 1 a, b, c#,  d