Applies ToRevolution Analytics

通过零或其他内容替换所有 NA 值就需要将数据从输入数据集使用函数输出数据集转换rxDataStep。替换所有 NA xdf 文件"AirlineDemoSmall.xdf"中的示例脚本如下所示︰

# Create a data frame with missing valuesset.seed(17)myDataF <- data.frame(x = rnorm(100), y = runif(100), z = rgamma(100, shape = 2))xmiss <- seq.int(from = 5, to = 100, by = 5)ymiss <- seq.int(from = 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[is.na(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

需要更多帮助?

需要更多选项?

了解订阅权益、浏览培训课程、了解如何保护设备等。

社区可帮助你提出和回答问题、提供反馈,并听取经验丰富专家的意见。