【问题标题】:How to convert text columns to numeric from a DBF file如何将文本列从 DBF 文件转换为数字
【发布时间】:2015-01-26 01:31:33
【问题描述】:

我有几个 DBF 文件需要稍后处理,但文件中的所有列都是字符。

允许我这样做的代码是这样的:

library("shapefiles")
library("data.table")
library("reshape2")
library("stringr")

as.numeric.factor <- function (parFactor)
{
  isAllNumerical <- all( suppressWarnings(!is.na(as.numeric( levels(parFactor)) )) == TRUE)
  if (isAllNumerical == TRUE)
  {
    parFactor <- as.numeric( as.character( parFactor ) )
  }
  else
  {
    parFactor <- FALSE
  }
  return (parFactor)
}

dbf.factorsToNumeric <- function (dataFrame, colsToConvert)
{
  for (colName in colsToConvert)
  {
    numColum <- as.numeric.factor( dataFrame[,colName] )
    if (class(numColum) != "logical")
    {
      colPosition <- match (colName, names(dataFrame))
      dataTypes <- attr(dataFrame, "data_types")
      dataTypes[colPosition] <- "N"
      attr(dataFrame, "data_types") <- dataTypes
      dataFrame[,colName] <- numColum
    }
  }
  return(dataFrame)
}

filePath <- file.choose() #assign file path to a variable
survey <- read.dbf( filePath , header = TRUE) #LoadFile
print(survey$header$num.records) # Print number of records

colNames <- names(survey$dbf) #get all column names in dataset
survey$dbf <- dbf.factorsToNumeric (survey$dbf, colNames) #convert required columns into numeric
outFilePath <- str_replace(filePath, ".dbf|.DBF", "2.dbf") #replace original filename to avoid overwriting
write.dbf(survey, outFilePath, FALSE) #write new file

无论如何我可以避免两次“as.numeric”转换吗?

谢谢。

【问题讨论】:

    标签: r dbf


    【解决方案1】:

    这是 tryCatch 的理想场所:

    as.numeric.factor <- function (x) {
        allNumeric  <-  TRUE
        # try to convert the levels to numeric
        tryCatch(vals  <-  as.numeric(levels(x)),
                 # if there is a warning, set `allNumeric` to FALSE
                 warning=function(err)
                     if(err$message == 'NAs introduced by coercion')
                         allNumeric<<-FALSE
                     #raise an error if the warning isn't the one we expected.
                     else stop(err))
        if(allNumeric)
            # if the levels are all numeric, return a numeric vector
            return(vals[unclass(x)])
        else
            # otherwise return the oringinal factor
            return(x)
    }
    

    【讨论】:

    • 谢谢!我不知道我可以在函数中使用函数。我在 R 方面没有太多经验。让我试试,我会告诉你的。
    猜你喜欢
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2013-10-13
    • 1970-01-01
    相关资源
    最近更新 更多