[R] How to subtract the counter i in for loop?

Berend Hasselman bhh at xs4all.nl
Fri Nov 30 09:09:27 CET 2012


On 30-11-2012, at 07:18, C W wrote:

> thanks, Berend.  Both of your code works great.  Is there a function that can do it?  
> 
> Something like this:
> x <- matrix(NA, nrow=15, ncol=2)
> for(i in 1:15){
>    x[i,] <- sample(c(NA, 20, 77), 2, prob=c(0.2, 0.3, 0.4))
> }
> 
> > x
> 
>       [,1] [,2]
> 
>  [1,]   NA   77
> 
>  [2,]   77   NA
> 
>  [3,]   NA   77
> 
>  [4,]   77   20
> 
>  [5,]   77   20
> 
>  [6,]   77   20
> 
>  [7,]   20   NA
> 
>  [8,]   77   20
> 
>  [9,]   77   NA
> 
> [10,]   77   NA
> 
> [11,]   77   20
> 
> [12,]   20   77
> 
> [13,]   NA   77
> 
> [14,]   77   20
> 
> [15,]   77   20
> 
> I want to have a column of 15 samples without NA's.  Is there an R function like ifelse()?
> 

It's not clear what you exactly want.
A matrix with 15 rows and some columns?
How do you want to remove the NA's in each column?
Then why don't you leave out the NA in the sample?

Berend




More information about the R-help mailing list