Posted to another thread a response to this posting ( and to all those who 
wanted R on an iPad, I say "forget it"
---------

The if function only takes an argument of length 1 (as the warning says):

?"if"

Many such confusions are resolved by looking at :

?ifelse

-- 
David
On Nov 1, 2011, at 4:45 PM, "M. Tran" <michellev.t...@gmail.com> wrote:

> I have a dataset called "results" that looks like this:
> 
> arrive  depart  intercept
>  1        1          1
>  1        2          1
>  1        3          1
>  1        2          2
>  1        3          2
>  1        3          3 
>  2        2          2
>  2        3          2
>  3        3          3
> 
> where arrive is the period of arrival, depart is the period of departure,
> and intercept is the period in which that person was counted.  I'm trying to
> construct the denominator for a likelihood function using the following
> function.  For the first row in "results", for example, I want the
> denominator to be the sum of all possible arrive/depart combinations an
> interceptor in period 1 could observe: exp(P_1_1) + exp(P_1_2) + exp(P_1_3)
> (i.e. P_arrive_depart).
> 
> get_denominator = function(intercept, periods_per_day)
>    {
>    denominator    =    array("(", nrow(results))
>    for (arrival in 1:periods_per_day)
>    {
>        for (departure in arrival:periods_per_day)
>        {
>            while (arrival <= intercept & intercept <= departure)
>            {
>        addition_to_denom    =    paste("P", arrival, departure, sep = "_")
>                if (nchar(denominator) == 1)
>                {
>        denominator        =    paste(denominator, "exp(", addition_to_denom, 
> ")", sep =
> "")
>                }
>                else
>                {
>        denominator        =    paste(denominator, " + exp(", 
> addition_to_denom, ")", sep =
> "")
>                }
>            }
>        }
>    }
>        denominator        =    paste(denominator, ")")
>        return(denominator)
>    }
> 
> 
> denominator    =    get_denominator(intercept        =    
> results[,"intercept"],
>                                    periods_per_day    =    3)
> 
> 
> I'm getting the following warning message:
> 
> In if (arrival <= intercept & intercept <= departure) { ... :
>  the condition has length > 1 and only the first element will be used.
> 
> As written, the code gives me the denominator for a period 1 interceptor for
> every single row!  
> 
> I'm having trouble figuring out how I should re-write this code.  Any
> suggestions would be greatly appreciated.
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/condition-has-length-1-for-LL-denominator-tp3965365p3965365.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to