1
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

1
  • A function stops running when it gets to the first return, so yours will be ending at return(Date). Maybe it will be easiest if you combine the data.frames inside the function and return that. Commented Sep 8, 2014 at 18:45

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.