我已经在一个表中读取了5列和200行到R - 我想有一个向量中的索引在我的第五列中具有负值的10行 - 是不知何故可能?从列中提取负值
我的表被称为r。 我知道我可以通过提取第五行:
r[,5]
,但我不知道我怎么能那么排除所有正值,或获得负值的指标列
我已经在一个表中读取了5列和200行到R - 我想有一个向量中的索引在我的第五列中具有负值的10行 - 是不知何故可能?从列中提取负值
我的表被称为r。 我知道我可以通过提取第五行:
r[,5]
,但我不知道我怎么能那么排除所有正值,或获得负值的指标列
# *** 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
很好...但是,我想要一个向量中的数值...所以只是在第五列中具有负值的行的行数 –
@TimHeinert请参阅编辑;你只需要用哪个() – mjv
啊,没看到你在最后加了答案。我正在删除我的答案和你的+1 –
看一些介绍R文本,例如http://cran.r-project.org/manuals.html,应该让你的答案。 –
是不是有一个快速单一的commadn留置权的事情 - 我知道它的介绍btu它woudl eb伟大的,如果有人可以简要地outlien它 –
是的,它可能使用简单的数据帧索引(条件)。发布一个可重现的例子,如果你尝试&仍然失败。 – Nishanth