[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 <-

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)))
           M[i,j] <- MAT[i,j]
           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

Thank you

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

For super low premiums, click here.

More information about the R-help mailing list