当引用的对象是R中的嵌套函数时,找不到“对象'...'

时间:2016-10-23 20:30:36

标签: r function dataframe

尝试在函数中嵌套函数,以便在接收数据框后返回R中的列表。但是马上就遇到了问题:

  

错误------ frqTbl< - function(df){:object'frqTbl'not found

有没有办法在函数定义之前定义一个函数变量?或者嵌套是不正确的?

经过测试:

data(diamonds, package = "ggplot2")
test <- diamonds[1:100,]
mstrFnct(test)

mstrFnct <- function(df){      
    output <- list()  
    frqTbl <- function(df){
        fctvr <- df[sapply(df,is.factor)]
        logicvr <- df[sapply(df,is.logical)] 
        nwDf <- data.frame(fctvr,logicvr)
        if(ncol(nwDf)>0){      
            freq <-list() 
            for (i in 1:ncol(nwDf)){ 
               freq[[i]] <- as.data.frame(table((nwDf)[,i]))
               names(freq[[i]])[1]=colnames(nwDf[i])
            } 
            return(freq)
        }
        else{
            print("There are no categorical or logical variables in the data   
            frame.")  
        }
    }
    output[[length(output)+1]] <- frqTbl(df)
    rSqd <- function(df){
        y <- df[sapply(df,is.numeric)]         
        if(ncol(y)>=2){ 
            c <- combn(colnames(y), 2) 
            vrPrs <- paste(c[1,], c[2,], sep = "-")
            m <- cor(y, method = "pearson")
            r <- m[which(lower.tri(m))] 
            vlus <- r^2
            df2 <- data.frame(vrPrs, values)
            names(df2) <- sub("^VrPrs$", "Variable Pairs", 
                names(df2)) 
            names(df2) <- sub("^vlus$", "R-Square", names(df2))
            format.data.frame(df2) 
            return(df2)
        }
        else{
            print(paste("This Data Frame does not have two or more numerical  
            columns to compute the Pearson correlation coefficient(s)."))
        }
    }
    output[[length(output)+1]] <- rSqd(df)
}

1 个答案:

答案 0 :(得分:1)

  

是否有某种方法可以定义一个变量之前的函数   功能定义?

没有。 (请参阅第一个代码块)

  

或者嵌套是不正确的?

其实没有。你刚搞砸了变量名。 (参见第二个代码块)

我建议使用以下代码来涵盖您的示例:

frqTbl <- function(df){

  fctvr <- df[sapply(df,is.factor)]
  logicvr <- df[sapply(df,is.logical)] 
  nwDf <- data.frame(fctvr,logicvr)

  if(ncol(nwDf)>0){ 

    freq <-list() 
    for (i in 1:ncol(nwDf)){ 

      freq[[i]] <- as.data.frame(table((nwDf)[,i]))
      names(freq[[i]])[1]=colnames(nwDf[i])
    } 
    return(freq)
  }
  else{
    print("There are no categorical or logical variables in the data   
            frame.")  
  }
}

rSqd <- function(df){

  y <- df[sapply(df,is.numeric)] 

  if(ncol(y)>=2){ 

    c <- combn(colnames(y), 2) 

    vrPrs <- paste(c[1,], c[2,], sep = "-")

    m <- cor(y, method = "pearson")

    r <- m[which(lower.tri(m))] 

    vlus <- r^2

    df2 <- data.frame(vrPrs, vlus)

    names(df2) <- sub("^vrPrs$", "Variable Pairs", 
                      names(df2)) 
    names(df2) <- sub("^vlus$", "R-Square", names(df2))


    format.data.frame(df2) 
    return(df2)

  }
  else{
    print(paste("This Data Frame does not have two or more numerical  
            columns to compute the Pearson correlation coefficient(s)."))
  }
}

mstrFnct <- function(df){

  output <- list()
  output[[length(output)+1]] <- frqTbl(df)
  output[[length(output)+1]] <- rSqd(df)

  return(output)
}

data(diamonds, package = "ggplot2")
test <- diamonds[1:100,]
mstrFnct(test)

但您也可以将函数定义打包到主函数中。像这样:

mstrFnct <- function(df){

  # create output list
  output <- list()

  # define function frqTbl()
  frqTbl <- function(df){

    fctvr <- df[sapply(df,is.factor)]
    logicvr <- df[sapply(df,is.logical)] 
    nwDf <- data.frame(fctvr,logicvr)

    if(ncol(nwDf)>0){ 

      freq <-list() 
      for (i in 1:ncol(nwDf)){ 

        freq[[i]] <- as.data.frame(table((nwDf)[,i]))
        names(freq[[i]])[1]=colnames(nwDf[i])
      } 
      return(freq)
    }
    else{
      print("There are no categorical or logical variables in the data   
          frame.")  
    }
  }

  # call function frqTbl() and store result in list
  output[[length(output)+1]] <- frqTbl(df)

  # define function rSqd()
  rSqd <- function(df){

    y <- df[sapply(df,is.numeric)] 

    if(ncol(y)>=2){ 

      c <- combn(colnames(y), 2) 

      vrPrs <- paste(c[1,], c[2,], sep = "-")

      m <- cor(y, method = "pearson")

      r <- m[which(lower.tri(m))] 

      vlus <- r^2

      df2 <- data.frame(vrPrs, vlus)

      names(df2) <- sub("^vrPrs$", "Variable Pairs", 
                        names(df2)) 
      names(df2) <- sub("^vlus$", "R-Square", names(df2))


      format.data.frame(df2) 
      return(df2)

    }
    else{
      print(paste("This Data Frame does not have two or more numerical  
            columns to compute the Pearson correlation coefficient(s)."))
    }
  }

  # call function rSqd() and store result in list
  output[[length(output)+1]] <- rSqd(df)

  return(output)
}

data(diamonds, package = "ggplot2")
test <- diamonds[1:100,]
mstrFnct(test)