Reading a line starting with a hash in a .txt file

The first line of my .txt file looks like this:

# 12 0.44 0.001 0.94444 123.3455 0.0000001 3432.0 2 1 1 12.2

These are numerical values ​​separated by a space, but the problem is that the line starts with a hash.

Can I read this first line from R even if it starts with a hash (without changing the file) ?

+4
source share
2 answers

You can try it scan. The comment symbol indicator is comment.charoff by default. And you can add nlines = 1to get only the first line from the file.

x <- "# 12 0.44 0.001 0.94444 123.3455 0.0000001 3432.0 2 1 1 12.2"
as.numeric(scan(text = x, what = "", nlines = 1)[-1])
# Read 12 items
#  [1]   12.0000000    0.4400000    0.0010000    0.9444400  123.3455000
#  [6]    0.0000001 3432.0000000    2.0000000    1.0000000    1.0000000
# [11]   12.2000000

So it should be fine if you replace text = xwith your file name.

+2

, , :

mydat <- read.table('test.txt', comment.char = '&', header = FALSE)

, , .

: , comment.char, : #

+2

All Articles