如果其他声明变坏了

时间:2012-10-13 03:41:24

标签: r

我在运行此程序时收到错误消息,是否有任何对任何人都很明显的事情?

 yo <-  function(x) {

      filt <- ddply(x, .(primer,day), summarise, count=sum(timepoints==0)) # this will tell you all primers that have a 0 hr time point by giveing a 1 in the count column



 if (any(filt$count) == 0)     { # this was the case once so I implemented this if else part

      filt <- filt[filt$count == 0,]
      include <-!(x$primer%in%filt$primer)&(x$day%in%filt$day) # all primers that have 0 hrs
      x <- x[include,] 
     ### for any given replicate, divide each timepoint by its zero hour 
     x <- ddply(x, .(primer),transform, foldInduction=realConc/realConc[timepoints==0])

}


  else {
x <- ddply(x, .(primer), transform, foldInduction=realConc/realConc[timepoints==0])
   }
  x[,-9]

  } 

2 个答案:

答案 0 :(得分:3)

是的,放置花括号。

我们鼓励你写

 if (cond) {
     code
 } else {
     more_code
 }

当解析器逐行进行时 - 除非你使用source()之类的东西,或者在构建包时完成解析并且文件被“整体”消耗而不是逐行消费

但作为一般规则:不要使用原始问题所显示的风格。

答案 1 :(得分:1)

好的,将我的评论推荐给答案。

any(filt$count) == 0没什么意义。为什么?与R中的所有逻辑强制一样,any将采用filt$count表示的数字,如果为零则返回true,如果为非零,则返回1。

> any(5)
[1] TRUE
Warning message:
In any(5) : coercing argument of type 'double' to logical

然而,一旦它是合乎逻辑的,你就可以通过将它与数字进行比较来将其强制转换为数字。那么你的陈述真正做的是查看filt$count是否为零(在这种情况下它返回TRUE),然后否定它。

> any( c(5,6,7) )==0
[1] FALSE
Warning message:
In any(c(5, 6, 7)) : coercing argument of type 'double' to logical
> any( c(5,6,0) )==0
[1] FALSE
Warning message:
In any(c(5, 6, 0)) : coercing argument of type 'double' to logical
> any( c(0) )==0
[1] TRUE
Warning message:
In any(c(0)) : coercing argument of type 'double' to logical

解决方案:不要这样做。