[R] rtriang using ifelse statement

David Winsemius dwinsemius at comcast.net
Sat Mar 17 22:48:44 CET 2012


On Mar 17, 2012, at 3:22 PM, Diann Prosser wrote:

> Thank you for that explanation and solution David.
>
> As a follow-up to ifelse working on vectors, earlier I tried to coerce
> (?correct term?) the matrix into a vector but I still received the  
> same
> result.

You can certainly "coerce" a matrix to a vector, since a matrix is  
really just a folded vector. There are several ways to do this.  
Removing the dimension attribute is another.

> Should the following work, or does as.vector not work that way,
> even though vect now prints as a vector?

As I see it you asked for 4 random triangular draws to fill in 6  
elements, so should not have been surprised when you got a warning,   
and you should note that the 5th and 6th are repeats of the random  
values 1 and 2. You gave it a conditional that was 6 elements long but  
a consequent and an alternate that were only 4 elements.

>
>> library(mc2d)
>> matrx <- matrix(c(2, 1, 1, 2, 2,1), nc=3)
>> vect <- as.vector(matrx)
>> *vect*
> [1] *2 1 1 2 2 1*
>> # rtriang from mc2d package: function (n, min = -1, mode = 0, max =  
>> 1)
>> dvect<- ifelse(vect==1, rtriang(4, min=0.001, mode=as.vector(vect),
>> max=2.001), rtriang(4, min=2, mode=as.vector(vect), max=3))
> Warning message:
> In rtriang(4, min = 2, mode = as.vector(vect), max = 3) : NaN in  
> rtriang
>> * dvect*
> [1] *2.3838913 0.5752171 0.3598679 2.0560875 2.3838913 0.5752171*
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/rtriang-using-ifelse-statement-tp4481037p4481113.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list