2011-08-31 18 views

答えて

2

ファイル全体を読み込み、テーブルヘッダや空行を解析したいと思うでしょう。私はあなたのtxtファイルのテーブルを変更した場合に/あなたが簡単に変更するために、あなたが設定し、それがスクリプトの一番上にあるようにヘッダーをvarにします。

+0

よろしくお願いいたします。だから私は全部を 'lines < - scan(inFile、what =" character "、sep =" \ n ")'と読みました。最初のテーブルは、最初の行のタイトルと最初の行のヘッダーと、最初の列のrow.namesを持ちます。テーブルのデータ部分は常に32行です。最初のテーブルを取得するにはどうしたらいいですか? – James

+2

私はreadLinesを使用していました(私は何を得るか知っているからです)。 'read.table(textConnection(lines [2:33])' –

1

シンプルなgoogle検索で返されました。 完全に私のために働いた。

> x <- readLines(textConnection("1 
+ Pietje 
+ I1 I2 Value 
+ 1 1 0.11 
+ 1 2 0.12 
+ 2 1 0.21 
+ 
+ 2 
+ Jantje 
+ I1 I2 I3 Value 
+ 1 1 1 0.111 
+ 3 3 3 0.333")) 
> closeAllConnections() 
> start <- grep("^[[:digit:]]+$", x) 
> mark <- vector('integer', length(x)) 
> mark[start] <- 1 
> # determine limits of each table 
> mark <- cumsum(mark) 
> # split the data for reading 
> df <- lapply(split(x, mark), function(.data){ 
+  .input <- read.table(textConnection(.data), skip=2, header=TRUE) 
+  attr(.input, 'name') <- .data[2] # save the name 
+  .input 
+ }) 
> # rename the list 
> names(df) <- sapply(df, attr, 'name') 
> df 
$Pietje 
    I1 I2 Value 
1 1 1 0.11 
2 1 2 0.12 
3 2 1 0.21 

$Jantje 
    I1 I2 I3 Value 
1 1 1 1 0.111 
2 3 3 3 0.333 

Source

関連する問題