Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
m
(Answer: new version of fillna added)
Line 49: Line 49:
 
}
 
}
  
objects.store(ograph)
+
# fillna takes a data.frame and fills the cells with NA with each level in that column.
 +
# object is the data.frame, marginals is a vector of columns (either column names or positions) that are to be filled.
 +
# This version of fillna accepts column positions (as the previous version) and also column names in marginals.
  
cat("The following objects are stored: ograph\n")
+
fillna <- function (object, marginals) {
 +
a <- dropall(object)
 +
if(!is.numeric(marginals)) marginals <- match(marginals, colnames(object))
 +
for (i in marginals) {
 +
a[[i]] <- as.factor(a[[i]])
 +
a1 <- a[!is.na(a[[i]]), ]
 +
a2 <- a[is.na(a[[i]]), ][-i]
 +
addition <- data.frame(A = levels(a[[i]]))
 +
colnames(addition) <- colnames(a)[i]
 +
a2 <- merge(addition, a2)
 +
a <- rbind(a1, a2)
 +
}
 +
return(a)
 +
}
 +
 
 +
objects.store(ograph, fillna)
 +
 
 +
cat("The following objects are stored: ograph, fillna\n")
  
 
</rcode>
 
</rcode>

Revision as of 10:28, 16 November 2013



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>