[R] Replace NAs in a data frame with elements randomly generated from the beta distribution

peter dalgaard pdalgd at gmail.com
Wed Apr 10 23:12:25 CEST 2013


On Apr 10, 2013, at 13:09 , Sibusiso Ndzukuma wrote:

> Hi there!
> 
> Please help me, I am trying to replace all NAs in a data frame with numbers
> randomly generated from the beta(0.1,1) distribution. Firstly I tried looping:
> 
> MAT <-
> c(0.68,9.86,1.29,0.25,5.28,1.13,1.66,0.41,2.65,0.00,11.5,32.67,0.98,3.06,2.97,0.51,13.62,1.23,0.00,1.79,5.89,0.00,10.36,1.20,2.06,0.16,0.00,0.00,7.95,3.82,0.64,0.93,0.73,0.00,5.43,1.45,1.36,1.61,2.02,0.00,10.36,1.51,1.04,1.93,1.40,0.00,6.03,1.30,1.74,7.07,0.00,0.00,9.20,0.19)
> 
> MAT <- data.frame(matrix(MAT, ncol = 6, byrow = FALSE))
> 
> MAT[MAT == 0] <- NA
> 
> M <- data.frame()
> 
> for (i in (1:nrow(MAT)))
> {
>     for(j in (1:ncol(MAT)))
>     {
>        if(!is.na(MAT))
>        {
>           M[i,j] <- MAT[i,j]
>        }
>        else
>           M[i,j] <- rbeta(1,0.1,1)
>     }
> 
> }
> But this does not work.
> 
> Then I tried this:
> 
> MAT[is.na(MAT)] <- rbeta(1,0.1,1) 
> 
> The problem with the second attempt is that the NAs are replaced with a value
> which is the same for all NA elements, yet I want them to be different. Please
> help! 
> 


You only asked for one value.... 

How about

N <- sum(is.na(MAT))
MAT[is.na(MAT)] <- rbeta(N, 0.1, 1) 

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list