Randomly insert NAs into dataframe proportionaly

后端 未结 6 1444
無奈伤痛
無奈伤痛 2020-11-29 12:07

I have a complete dataframe. I want to 20% of the values in the dataframe to be replaced by NAs to simulate random missing data.

A <- c(1:10)
B <- c(1         


        
6条回答
  •  渐次进展
    2020-11-29 13:03

    You can unlist the data.frame and then take a random sample, then put back in a data.frame.

    df <- unlist(df)
    n <- length(df) * 0.15
    df[sample(df, n)] <- NA
    as.data.frame(matrix(df, ncol=3))
    

    It can be done a bunch of different ways using sample().

提交回复
热议问题