2013-02-18 143 views
-5

我想返回的对象的值而不是TRUE或FALSE,虽然我挣扎:到目前为止我有一个数据列表,我可以调用该列表返回值,罚款。我也想返回大于0的列表值。我不只是想要True或FALSE。那么我要总结高于零....值R字符串到对象

with_Gain = (Count_Return1 > 0) 
get("with_Gain") 

请指教,多谢

+6

什么呢,你所提供的返回码?你想要返回什么?你能提供一个完整的例子,输入和期望的输出? – Justin 2013-02-18 15:14:29

+0

Justin-Just提出了一个TRUE和FALSE的列表,它基本上是正确的 - 虽然我想要TRUE的值。 – VivaNosh 2013-02-18 15:19:56

+0

Josh-这只是返回TRUE和FALSE ... – VivaNosh 2013-02-18 15:20:57

回答

0

您可以按住Shift左括号这样的:

(with_Gain = Count_Return1 > 0) 
[1] TRUE 

当然这个假设您已经定义Count_Return1 ..

编辑

为了获取对象的值,你这样做

Count_Return1[Count_Return1 > 0] 

,并获得条件

which(Count_Return1 > 0) 

的索引。例如:我觉得你是

set.seed(1234) 
> Count_Return1 <- sample(c(-1,1),10,rep=T) 
> Count_Return1[Count_Return1 > 0] 
[1] 1 1 1 1 1 1 1 
> Count_Return1 > 0 
[1] FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE 
2

寻找which()

Count_Return1 <- c(3,2,-1) 

Count_Return1[which(Count_Return1 > 0)] 
#[1] 3 2 
#returns the values above 0 

which()这里并未真正Count_Return1[Count_Return1 > 0]需要会做同样的工作。

但是如果你想索引不是值那么使用:

which(Count_Return1 > 0) 
#[1] 1 2 
#returns the index for the values above 0 
+6

这是一个很好的阅读尝试。我们将会看到OP是否确认... – 2013-02-18 15:27:52

+0

未经批准的评论的本位。 – VivaNosh 2013-02-18 15:31:37

+0

谢谢@ JoshO'Brien是的,我在'#comments'中说过了,也许我应该让他们更清楚 – 2013-02-18 15:32:40