【问题标题】:Naming inside a loop with deparse使用 deparse 在循环内命名
【发布时间】:2013-01-14 13:35:07
【问题描述】:

我尝试编写几个表来计算数据框中不同列的 colMeans。该功能工作正常,但命名是一个问题:

function.test<-function(a,b,df)
  {for (i in a:b) 
     write.table(
      as.data.frame(
        lapply(
          split(df, df[,i]), colMeans)
        ),
      file=paste("E:\\R\\Outputs\\colMeans",deparse(i),deparse(df),".csv"
             ),
      sep=",",row.names=T,col.names=NA
      )
   }

仅解析“i”可以正常工作,但无法解析“df”。我收到以下错误消息:

   Error in file(file, ifelse(append, "a", "w")) : 
   invalid 'description' argument
   In addition: Warning message:
   In if (file == "") file <- stdout() else if (is.character(file)) { :
     the condition has length > 1 and only the first element will be used

我猜是这样的,因为“i”随着循环而变化,而 df 始终保持不变。有谁知道如何使用 i 和 df 命名它。因为如果不是,我将不得不为每个数据帧编写一个单独的函数,这将是很多代码......

【问题讨论】:

    标签: loops naming


    【解决方案1】:

    这次尝试和错误帮助了我们:

    deparse(i),deparse(substitute(df))  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 2021-12-24
      • 2012-02-28
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多