では?read.table
、次のように述べられています。
The number of data columns is determined by looking at the first five lines of input
(or the whole file if it has less than five lines), or from the length of col.names
if it is specified and is longer. This could conceivably be wrong if fill or
blank.lines.skip are true, so specify col.names if necessary (as in the ‘Examples’).
パラメータを使用する必要がありfill
、一部の txt ファイルには、5 行目以降に列数が最も多い行が含まれている場合があります。ヘッダーがなく、インポート後に col.names が定義されるという理由だけでヘッダーを使用できないため、R がファイル全体に使用したこれらの 5 行を変更したいと思います (私は使用しません)。私が得ることができる速度の損失に注意してください)。なにか提案を?ありがとう!
編集:
のコードでこれを見つけましたread.table
if (skip > 0L)
readLines(file, skip)
nlines <- n0lines <- if (nrows < 0L)
5
else min(5L, (header + nrows))
lines <- .External(C_readtablehead, file, nlines, comment.char,
blank.lines.skip, quote, sep)
nlines <- length(lines)
5
上記のコードの 4 行目の数字を変更することはできますか? read.table
それは行動に何らかの副作用をもたらすでしょうか?
編集2:
私は現在この方法を使用しています
maxCol <- max(sapply(readLines(filesPath), function(x) length(strsplit(x, ",")[[1]])))
列の最大数を持ち、結果を入れてダミーcol.names
のように作成しますpaste0("V", seq_len(maxCol))
。read.table
それを選択できる可能性のある別のものを持つ価値があると思いますか?