2013-04-15 72 views
0

我已經在一個表中讀取了5列和200行到R - 我想有一個向量中的索引在我的第五列中具有負值的10行 - 是不知何故可能?從列中提取負值

我的表被稱爲r。 我知道我可以通過提取第五行:

r[,5] 

,但我不知道我怎麼能那麼排除所有正值,或獲得負值的指標列

+2

看一些介紹R文本,例如http://cran.r-project.org/manuals.html,應該讓你的答案。 –

+0

是不是有一個快速單一的commadn留置權的事情 - 我知道它的介紹btu它woudl eb偉大的,如果有人可以簡要地outlien它 –

+0

是的,它可能使用簡單的數據幀索引(條件)。發佈一個可重現的例子,如果你嘗試&仍然失敗。 – Nishanth

回答

3
# *** Get some test data *** 
> con <- textConnection(Lines <- " 
+ First, Last, Email, CustomerSince, Balance, RiskFactor 
+ Alan, Appleton, [email protected],1992, -100, 3 
+ Bob, Bates, [email protected],1997, 231, 2 
+ Charles, Clemson, [email protected],2001, -4.2, 1 
+ Dennis, Delgado, [email protected],2004, 27, 1 
+ Ernesto, Ellonka, [email protected],2010, 40.5, 6 
+ ") 
> x <- read.csv(con) 
> close(con) 
# *** That's what our data looks like. 5th column is numeric 
> x 
    First  Last    Email CustomerSince Balance RiskFactor 
1 Alan Appleton  [email protected]   1992 -100.0   3 
2  Bob  Bates  [email protected]   1997 231.0   2 
3 Charles Clemson [email protected]   2001 -4.2   1 
4 Dennis Delgado  [email protected]   2004 27.0   1 
5 Ernesto Ellonka [email protected]   2010 40.5   6 

# *** And here's how to get a vector of all indexes of the Balance column 
# *** that have a negative value 
> x["Balance"] < 0 
    Balance 
[1,] TRUE 
[2,] FALSE 
[3,] TRUE 
[4,] FALSE 
[5,] FALSE 
> 
# *** And here's how to directly exclude the positive balance from the list 
> x[x$Balance < 0, ] 
First  Last    Email CustomerSince Balance RiskFactor 
1 Alan Appleton  [email protected]   1992 -100.0   3 
3 Charles Clemson [email protected]   2001 -4.2   1 

# to just get the list of indexes that match that criteria, you can use which() 
> which(x$Balance < 0) 
[1] 1 3 
+0

很好...但是,我想要一個向量中的數值...所以只是在第五列中具有負值的行的行數 –

+0

@TimHeinert請參閱編輯;你只需要用哪個() – mjv

+0

啊,沒看到你在最後加了答案。我正在刪除我的答案和你的+1 –