Darf ich vorschlagen, eine erste Funktion (ggNAadd) ausgelegt, dies zu tun, und verbessert sie mit einer zweiten Funktion grafische Verteilung der NAs erstellt (ggNA) Bereitstellen
Was ist sauber ist die Möglichkeit, entweder einen Anteil einer festen Anzahl von NAs einzugeben.
ggNAadd = function(data, amount, plot=F){
temp <- data
amount2 <- ifelse(amount<1, round(prod(dim(data))*amount), amount)
if (amount2 >= prod(dim(data))) stop("exceeded data size")
for (i in 1:amount2) temp[sample.int(nrow(temp), 1), sample.int(ncol(temp), 1)] <- NA
if (plot) print(ggNA(temp))
return(temp)
}
Und das Plotten Funktion:
ggNA = function(data, alpha=0.5){
require(ggplot2)
DF <- data
if (!is.matrix(data)) DF <- as.matrix(DF)
to.plot <- cbind.data.frame('y'=rep(1:nrow(DF), each=ncol(DF)),
'x'=as.logical(t(is.na(DF)))*rep(1:ncol(DF), nrow(DF)))
size <- 20/log(prod(dim(DF))) # size of point depend on size of table
g <- ggplot(data=to.plot) + aes(x,y) +
geom_point(size=size, color="red", alpha=alpha) +
scale_y_reverse() + xlim(1,ncol(DF)) +
ggtitle("location of NAs in the data frame") +
xlab("columns") + ylab("lines")
pc <- round(sum(is.na(DF))/prod(dim(DF))*100, 2) # % NA
print(paste("percentage of NA data: ", pc))
return(g)
}
Welche (mit ggplot2 als grafische Ausgabe) ergibt:
ggNAadd(df, amount=0.20, plot=TRUE)
## [1] "percentage of NA data: 20"
## A B c
## 1 1 11 21
## 2 2 12 22
## 3 3 13 23
## 4 4 NA 24
## ..
Natürlich, wie bereits erwähnt, Sie, wenn zu viele NAs fragen, wird der tatsächliche Prozentsatz wegen Wiederholungen fallen.
Sie meinen 15% der einzelnen Variablen? oder Gesamtbeobachtungen? – Robert
20% ist in Ordnung (d. H. 6 des Werts sollte NA sein) – Filly
Sie können diese Antwort, die Ihnen genaue Anteile von NA gibt, überprüfen: https://stackoverflow.com/q/39513837/3871924 – agenis