【发布时间】: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 命名它。因为如果不是,我将不得不为每个数据帧编写一个单独的函数,这将是很多代码......
【问题讨论】: