Skip to content
Back to formatted view

Raw Message

Message-ID: <1385661427.78119.YahooMailNeo@web142604.mail.bf1.yahoo.com>
Date: 2013-11-28T17:57:07Z
From: arun
Subject: Adding NA values in random positions in a dataframe

Hi,
One way would be:
?set.seed(42)
?dat1 <- as.data.frame(matrix(sample(c(1:5,NA),50,replace=TRUE,prob=c(10,15,15,20,30,10)),ncol=5))
set.seed(49)
?dat1[!is.na(dat1)][ match( sample(seq(dat1[!is.na(dat1)]),length(dat1[!is.na(dat1)])*(0.20)),seq(dat1[!is.na(dat1)]))] <- NA
length(dat1[is.na(dat1)])/length(unlist(dat1))
#[1] 0.28

A.K.


Hello, I'm quite new at R so I don't know which is the most efficient 
way to execute a function that I could write easily in other languages. 

This is my problem: I have a dataframe with a certain numbers of
 NA (approximately 10%). I want to add other NA values in random 
positions of the dataframes until reaching an overall proportions of NA 
values of 30% (clearly the positions with NA values don't have to 
change). I tried looking at iterative function in R as apply or sapply 
but I can't actually figure out how to use them in this case. Thank you.