2010-09-04 23 views
5

我已經習慣了perl,並且使用了新的R.我知道您可以使用read.table()來讀取整個表格,但我不知道如何使用R解析輸入文件中的單行。R中的基本輸入文件解析

具體來說,什麼是相當於下面的Perl片段:

open my $fh, $filename or die 'can't open file $filename'; 
my $line = <$fh>; 
my ($first, $second, $third) = split ("\t", $line); 

回答

3

到類似上面會:

filename <- 'your/file/name/here' 
fh <- file(filename, open='rt') 
line <- readLines(fh, n=1) 
tmp <- strsplit(line, "\\t") 
first <- tmp[[1]][1]; second <- tmp[[1]][2]; third <- tmp[[1]][3] 

文件函數創建該文件的連接,並打開它,開放是可選的,但如果你不要打開文件,那麼當你讀它時,它會打開,然後再次關閉文件,如果你打開文件,那麼它保持打開狀態,下一次讀取繼續從前一次的位置開始(與Perl將做的最接近的匹配以上)。

readLines函數將讀取指定數量的行(在這種情況下爲1) 然後strsplit的作用基本上與Perl拆分函數相同。 R沒有像Perl那樣的多重賦值(通常最好只是將結果保持在一起,而不是分割爲多個全局變量)。

+0

一個警告 - 這會將整個文件加載到內存中並拆分它的所有行。如果文件很大,而且只需要前三個元素,這當然不是一個好主意。 – mbq 2010-09-05 11:09:35

+0

對於一個小文件,它可能會讀取整個文件,但對於較大的文件,它只會讀取到內存中的一部分,因爲您繼續從該文件讀取它會抓取更多的塊。 – 2010-09-06 16:58:15

1

在一般情況下,你應該使用scan要做到這一點,或者在更復雜的情況下,與readLines讀取整個文件,並手動解析它strsplit s,grep s和東西。

你的情況:

scan(filename,character(0),nmax=3)->d 
first<-d[1];d[2]->second;third<-d[3] 
0

只是爲了展示另一種方式來做到這一點(假設你的輸入是「溫度/ 3.txt」):

> d <- read.csv("temp/3.txt", sep="\t", stringsAsFactors=F, header=F, nrows=1) 
# Show the default column names: 
> colnames(d) 
[1] "V1" "V2" "V3" 
# Assign the requested column names 
> colnames(d) <- c("first", "second", "third") 
# Show the current structure of d 
> d 
    first second third 
1  1  2  3 
# Probably not recommended: Add the columns of d to the search path 
> attach(d) 
> first 
[1] 1 
# Clean up: 
> detach(d) 

我想上面解決你的問題而言最重要的部分是剛剛

nrows=1 

它告訴它解析一行輸入。 (在read.csv下面最後只是調用掃描。)