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

Sibusiso Ndzukuma sibusisondzukuma at webmail.co.za
Wed Apr 10 13:09:06 CEST 2013


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! 

Thank you
Sibu



____________________________________________________________
South Africas premier free email service - www.webmail.co.za 

For super low premiums, click here.
http://www.dialdirect.co.za/landing/oct/1/?vdn=15752



More information about the R-help mailing list