1

I try to write several tables calculating the colMeans over diffrent columns within a dataframe. The function works fine but the naming is a problem:

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
      )
   }

Deparsing only "i" works fine but deparsing "df" doesn't work. I get the follwoing error message:

   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 guess it's like that because "i" changes with the loop and df stays always the same. Has anyone an idea how to name it using i and the df. Because if not I would have to write a seperate function for each dataframe and that will be a lot of code...

4

1 回答 1

0

好的试验和错误这次有帮助:

deparse(i),deparse(substitute(df))  
于 2013-01-14T13:50:18.403 回答