Leprechault
Leprechault

Reputation: 1823

Multiple functions in only one

I would like to create a multiple functions inside an one function, in my case the function was named MYsummary, with three functions inside, but doesn't work. Im my code:

Artificial data set

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)

The first function identifed the peoples, in the who variable:

Identifed people

people<-function(x, db = tableFE) {
     x<-NULL
     results <- x
     x<-unique(db[,1])
     results <- x
     results
}
PER<-people(db = tableFE)

--

print(PER)

The second fuction gives the time period:

Function time

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)

and the third the mean:

MD<-mean(tableFE[,3])
print(MD)

     But, whem I try to merge the three function in one for create MYsummary to display 

all the three results, doen't mach the results, see below:

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

Someone could help me?

Thanks,

Alexandre

Upvotes: 0

Views: 593

Answers (1)

DeveauP
DeveauP

Reputation: 1237

You define functions inside of your vector, and I don't think this can work.

However you can solve this by doing:

MySummary<-function(x){
people<-function(...){}
TIME<-function(...){}
MD<-function(...){}

result<-list(people(x),TIME(x),MD(x))
return(result)

}

Would this be the output you were looking for?

Upvotes: 2

Related Questions