2013-07-17 57 views
1

我以前收到很多幫助,但我遇到了另一個問題,並想知道是否有人會有任何見解。將帶矩陣轉換爲幀並省略某些行?

previous post,我寫的,我有一個數據集(它實際上有大約50行),我們稱之爲「泰晤士報」:

> Times <- read.csv(「Times.csv」, stringsAsFactors=FALSE, header=TRUE) 
> Times 

Num  Start   End 
1 00:09:41 00:25:025 
2 00:11:21  00:41:32 
3 00:34:39  00:58:01 

然後,爲了找到重合的時間間隔,有人建議我創建一個帶矩陣 - 比較所有的行。

Overlap <- outer (Times$Start, Times$End, function (x,y) y > x) 
Overlap [upper.tri (Overlap) | col (Overlap) = = row(Overlap)] <- NA 
Overlap 

     [,1] [,2] [,3]   
[1,]  NA  NA  NA 
[2,] TRUE  NA  NA 
[3,] FALSE TRUE  NA 

所以在這一點上,我知道這行重疊,但我非常希望有類似我的原始數據幀輸出,但不包括不重疊的任何其他行的行。

有什麼辦法可以省略不包含TRUE的行嗎?是否有可能將其轉換回數據框?

感謝您提供任何幫助!

回答

1

排除不與其他行重疊的行。

Times[rowSums(is.na(Overlap)) < ncol(Overlap),] 

編輯

既然你在重疊矩陣下部

Overlap [upper.tri (Overlap) | col (Overlap) = = row(Overlap)] <- NA 

您可以跳過這一步,使用原始重疊的下部,使這個簡單的只是有興趣解決方案:

Overlap <- outer (Times$Start, Times$End, function (x,y) y > x) 
Times[rowSums(lower.tri(mdat)) >0 ,] 
+1

+1 for rowSums(我猜這裏也可以使用.rowSums),但是我認爲你需要對Times df進行子集劃分。 –

+0

@ SimonO101當然是。我需要子集Times。這是一個錯字。 – agstudy

+1

哇,這很好!感謝所有的幫助。希望有一天我能幫助像我這樣的新手。 – user2585431

1

......怎麼樣....

exc <- apply(Overlap , 1 , function(x) all(is.na(x))) 

nonoverlap <- Times[ ! exc , ] 

基本上我們看看你的Overlap矩陣中的每一行,返回TRUE如果所有值都NA。然後,我們使用它來對Times數據幀進行子集排除,其中排除了Overlap中全部爲NA的那些行。