data.frame的lapply函数出错

时间:2018-04-26 15:19:54

标签: r

我有一个data.frame" df"有几个字符列。我需要根据每个char列的最大长度设置长度限制。

我试着这样做:

SetOraStrLength <- function(DfColumn){

  MaxStrLength <- as.integer(0)

  if (typeof(DfColumn) == "character")
  {
    MaxStrLength <- max(nchar(DfColumn, allowNA = FALSE))
    if (MaxStrLength > 0 & MaxStrLength <= 25){
      attr(DfColumn, "ora.encoding") <- "UTF-8"
      attr(DfColumn, "ora.maxlength") <- 25
    } else if (MaxStrLength > 25 & MaxStrLength <= 50) {
      attr(DfColumn, "ora.encoding") <- "UTF-8"
      attr(DfColumn, "ora.maxlength") <- 50
    } else if (MaxStrLength > 50 & MaxStrLength <= 150) {
      attr(DfColumn, "ora.encoding") <- "UTF-8"
      attr(DfColumn, "ora.maxlength") <- 150
    } else {
      attr(DfColumn, "ora.encoding") <- "UTF-8"
      attr(DfColumn, "ora.maxlength") <- 255
    }
  }

}

然后:

lapply(df, SetOraStrLength)

最后,我有错误:

  

if(MaxStrLength&gt; 0&amp; MaxStrLength&lt; = 25){:缺失   需要TRUE / FALSE的值来自:FUN(X [[i]],...)

有什么问题?

0 个答案:

没有答案