How to replace all NAs in an xdf file

For replacing all NA values by zero or other content it is necessary to transform data from an input data set to an output data set using function 


A sample script for replacing all NA in xdf file "AirlineDemoSmall.xdf" is shown below:

# Create a data frame with missing valuesset.seed(17)myDataF <- data.frame(x = rnorm(100), y = runif(100), z = rgamma(100, shape = 2))xmiss <- = 5, to = 100, by = 5)ymiss <- = 2, to = 100, by = 5)myDataF$x[xmiss] <- NAmyDataF$y[ymiss] <- NA# Convert into a xdfmyDataNA<-file.path(getwd(),"myDataNA.xdf")trsfxdf<-rxDataStep(inData=myDataF,outFile=myDataNA,overwrite=TRUE)writeLines("\n\nXdf Generated with random NA values")print(rxGetInfo(myDataF, n = 15)$data)     # Test ouput data #### Use from here if there is an existing xdf.## replace myDataNA with your xdf file##writeLines("\n\nVariables that contains NA values (Missing Observations)")(mySum <- rxSummary(~., data = myDataNA)$sDataFrame)# Find variables that are missingtransVars <- mySum$Name[mySum$MissingObs > 0]print(transVars) #Test detected variables# create a function to replace NA vals with meanNAreplace <- function(dataList) {        replaceFun <- function(x) {              x[] <- replaceValue              return(x)        } dataList <- lapply(dataList, replaceFun) return(dataList)}#myDataRMV<-file.path(getwd(),"myDataRMV.xdf")       # Replace Missing Value trsfxdf<- rxDataStep(inData = myData1, outFile = myDataRMV,     transformFunc = NAreplace,      transformVars = transVars,     transformObjects = list(replaceValue = "REPLACED MISSING VALUE"),     overwrite=TRUE)writeLines("\n\nTransformed xdf with NA replaced by Value")print(rxGetInfo(myDataRMV, n=15)$data)     # Test output data
Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

Id. de artículo: 3104212 - Última revisión: 10/29/2015 06:10:00 - Revisión: 1.0

Revolution Analytics

  • KB3104212