Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if command to test for integer(0)

Tags:

r

I'm using a command to return the points at which participants reach 8 contiguous responses in a row. The command is:

 test <- which( rle(goo)$values==1 & rle(goo)$lengths >= 8)

where:

 goo <- c(1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0)

if the participant never achieves 8 contiguous responses i'd like to set the variable "test" to equal -1. As it stands, the command returns integer(0) when 8 contiguous responses in a row are not found. I've tried writing an if command but can't seem to get it right.

Thanks in advance,

Will

like image 227
user678493 Avatar asked May 15 '11 01:05

user678493


1 Answers

If test is integer(0) then its length is 0. You can also coerce it to logical with !

length(test)
0
!(length(test)
TRUE    # and would be FALSE for any vector with normal length
> !(length( c(1,2,3) ))
[1] FALSE

So:

> if ( !length(test) ) {test<- -1} 

> test
[1] -1
like image 185
IRTFM Avatar answered Oct 15 '22 18:10

IRTFM