我正在测试R中的一个简单函数,该函数可以将时间序列对象转换为数据帧。然而,代码在函数外部运行良好,但在函数内部,它在对象中给出了错误。
>fx<-function(AMts) {
x<-as.data.frame(AMts)
return(x)
}
>fx()我希望在我的环境中有data.frame x,但我得到了Error in as.data.frame(AMts) : argument "AMts" is missing, with no default
发布于 2019-09-12 23:21:20
如果它在一个函数中,你需要使用"<<-“作为赋值操作符,而不是传统的"<-”。<<-告诉R在函数运行完成后保留对象。
>fx<-function(AMts) {
x<<-as.data.frame(AMts) # "<<-" is what saves "x" in your environment
return(x) # remove this line; this prints data frame "x" to the console, but it doesn't save it
}
>fx(AMts)编辑:正如评论者已经指出的,你没有在你的函数中包含任何参数。上面我用fx( AMts )来说明你也需要把AMts传递给这个函数。
https://stackoverflow.com/questions/57909533
复制相似问题