user3292373
user3292373

Reputation: 533

R calling function within an function

Airlines_Dataset <- function(Number_Of_Rows){

#Departure Date
day.start <- "2009/01/01"
day.end <- "2014/04/25"

Departure_Date <- function(day.start,day.end,size) { //Generates Random dates
    dayseq <- seq.Date(as.Date(day.start),as.Date(day.end),by="day")
    dayselect <- sample(dayseq,size,replace=TRUE)
    Date <- dayselect
}
Date <- data.frame(Departure_Date(day.start,day.end,size=Number_Of_Rows))
return(Date)


#Origin
Origin_Func <- function(Number_Of_Rows,...){
Origin <- (sample(c('IND','ISP','JAN','JAX','IND','ISP',
    'LAS','LAX','LBB','LIT','MAF','MCI','MCO','MDW','MHT',
'MST','MCO','OAK','OAC','OMA','OMT','ORF','PBI','PDX',
'PHL','PHX','PIT','PVD','RDU','RNO','RSW','SAN','SAT',
'SDF','SEA','SFO','SJC','SLC','SMF','SNA','STL','TPA',
'TUL','TUS','ABQ','IAD'),Number_Of_Rows, replace=TRUE))

Origin <- data.frame(Origin)
/*Given below is source code of origin*/   
 Origin_Exec <-     system.time((sample(c('IND','ISP','JAN','JAX','IND','ISP',
    'LAS','LAX','LBB','LIT','MAF','MCI','MCO','MDW','MHT',
    'MST','MCO','OAK','OAC','OMA','OMT','ORF','PBI','PDX',
    'PHL','PHX','PIT','PVD','RDU','RNO','RSW','SAN','SAT',
    'SDF','SEA','SFO','SJC','SLC','SMF','SNA','STL','TPA',
    'TUL','TUS','ABQ','IAD'),Number_Of_Rows, replace=TRUE)))
}
/*Some static origin codes that generates random strings from the list given above*/
return(Origin)

}

am calling the function as given below

Airlines_Result <- Airlines_Dataset(1000)

When I call the above functions only one function is invoked that is date and tine origin is not invoked.

Final Step is to combine the data frames

Airlines_Data <- data.frame(Origin,Date)

Anyone please help to execute the code in proper format I want origin and date to be present in single function call

Upvotes: 1

Views: 1815

Answers (1)

MasterJedi
MasterJedi

Reputation: 1638

I reordered your code and added some lines. You do not need to have nested functions in your case. To call a function within a function write the inner functions separately. The outer function call all other functions:

Departure_Date <- function(day.start,day.end,size) 
{ 
  dayseq <- seq.Date(as.Date(day.start),as.Date(day.end),by="day")
  dayselect <- sample(dayseq,size,replace=TRUE)
  Date <- dayselect

  return(Date)

}


  #Origin
Origin_Func <- function(Number_Of_Rows)
{
    Origin <- (sample(c('IND','ISP','JAN','JAX','IND','ISP',
                        'LAS','LAX','LBB','LIT','MAF','MCI','MCO','MDW','MHT',
                        'MST','MCO','OAK','OAC','OMA','OMT','ORF','PBI','PDX',
                        'PHL','PHX','PIT','PVD','RDU','RNO','RSW','SAN','SAT',
                        'SDF','SEA','SFO','SJC','SLC','SMF','SNA','STL','TPA',
                        'TUL','TUS','ABQ','IAD'),Number_Of_Rows, replace=TRUE))

    return(Origin)
}


Airlines_Dataset <- function(day.start ="2009/01/01", day.end = "2014/04/25", Number_Of_Rows=1000)
{  
  Data <- data.frame(Departure_Date(day.start,day.end,Number_Of_Rows),Origin_Func(Number_Of_Rows))
  names(Data) = c("Date", "Origin")
  return(Data)
}


Airlines_Result = Airlines_Dataset()

Upvotes: 1

Related Questions