我想在一个函数内创建一个多个函数,在我的例子中函数是 名为MYsummary,里面有三个函数,但不起作用。我是我的代码:
who<-rep(c("JOSE","CARLOS"),40)
dates<-rep(as.Date(c("2007-06-22", "2004-02-13")),40)
var1<-rnorm(80)
tableFE<-data.frame(who, dates,var1)
head(tableFE)
第一个函数在who变量中标识了人们:
people<-function(x, db = tableFE) {
x<-NULL
results <- x
x<-unique(db[,1])
results <- x
results
}
PER<-people(db = tableFE)
print(PER)
第二个功能给出时间段:
timeFE<-function(x, db = tableFE) {
x<-NULL
minT<-min(db[,2])
maxT<-max(db[,2])
results <- x
x<-cbind(minT,maxT)
colnames(x)<-c("start","end")
results <- x
results
}
TIM<-timeFE(db = tableFE)
print(TIM)
和第三个意思是:
MD<-mean(tableFE[,3])
print(MD)
But, whem I try to merge the three function in one for create MYsummary to display
所有三个结果,都没有结果,见下文:
MYsummary<-function(x, db = tableFE) {
c(
## First function
people<-function(x, db = tableFE) {
x<-NULL
results <- x
x<-unique(db[,1])
results <- x
results
print(results)
}
,
## Função do período
timeFE<-function(x, db = tableFE) {
x<-NULL
minT<-min(db[,2])
maxT<-max(db[,2])
results <- x
x<-cbind(minT,maxT)
colnames(x)<-c("start","end")
results <- x
results
print(results)
}
,
MD=mean(tableFE[,3])
)
}
MYsummary(tableFE)## Doesn't work
有人可以帮助我吗?
谢谢,
亚历山大
答案 0 :(得分:2)
你定义了向量中的函数,我认为这不起作用。
但是你可以通过这样做来解决这个问题:
MySummary<-function(x){
people<-function(...){}
TIME<-function(...){}
MD<-function(...){}
result<-list(people(x),TIME(x),MD(x))
return(result)
}
这会是您要找的输出吗?