# Exercise 13 - Working with functions

### Function Definitions

# loadDataFile - Reads the data file into a data frame
# No parameters passed to the function
# Returns the data frame

loadDataFile<-function()
{
  # Load the csv file into a temporary data frame tempFrame
  tempFrame<-read.csv("SP13/SolarFlares.csv", header=TRUE)
  
  # Return tempFrame to the main script
  return(tempFrame)
  
} # End of loadDataFile

# extractSunspotClass - Extracts the $SunspotClass column into a vector
# Data frame df passed to the function
# Returns the tempVector

extractSunspotClass<-function(df)
{
  # Save the %SunspotClass column to vector tempVector
  tempVector<-df$SunspotClass
  
  # Return tempVector to the main script
  return(tempVector)
  
} # End of extractSunspotClass

# extractSpotSize - Extracts the $SpotSize column into a vector
# Data frame df passed to the function
# Returns the tempVector

extractSpotSize<-function(df)
{
  # Save the %SpotSize column to vector tempVector
  tempVector<-df$SpotSize
  
  # Return tempVector to the main script
  return(tempVector)
  
} # End of extractSpotSize

# extractSpotDistribution - Extracts the $SpotDistribution column into a vector
# Data frame df passed to the function
# Returns the tempVector

extractSpotDistribution<-function(df)
{
  # Save the %SpotDistribution column to vector tempVector
  tempVector<-df$SpotDistribution
  
  # Return tempVector to the main script
  return(tempVector)

} # End of extractSpotDistribution

# countElements - Count how many times each element of v1 occurs in v2
# Vector v1 is the unique elements in v2
# Return a vector of numbers representing the counts

countElements<-function(v1,v2)
{
  # This will create a vector of numbers representing the number of times
  # each element of v1 appears in v2
  # Each iteration of the loop will add a new element to this vector
  tempCount<-NULL
  
  # Loop for all elements in v1
  for(i in v1)
  {
    # This will count in a loop, so set up a counter currentValue to 0
    currentValue<-0
    
    # Loop for all elements in v2
    for(j in v2) 
    {
      
      # If the current element of v1 is the same as that of v2 then
      # increment the currentValue counter
      if(i==j) currentValue<-currentValue+1
    } # End of v2 loop
    
    # Append the currentValue counter to the tempCount vector 
    tempCount<-c(tempCount,currentValue)
    
  } # End of v1 loop
  
  # Return tempCount to the calling function
  return(tempCount)
}

# createSummary - Create data frame of show the values and count of values
# Vector v passed to the function
# Return the new data frame

createSummary<-function(v)
{
  # Identify the unique elements of the vector v and place in tempValues
  tempValues<-unique(v)
  
  # Call countElements to calculate the number of occurrences of each element
  # and place in tempCount. Pass in tempElements and v
  tempCount<-countElements(tempValues, v)
  
  # Build tempDF from tempValues and tempCount
  tempDF<-data.frame(Values=tempValues, Count=tempCount)
  
  # Return tempDF to the main script
  return(tempDF)
  
}


### Main Script  ###

# Create data frame solarFlares

solarFlares<-loadDataFile()

# Extract SunspotClass into a vector v_sunspotClass

v_sunspotClass<-extractSunspotClass(solarFlares)

# Extract SpotSize into a vector v_spotSize

v_spotSize<-extractSpotSize(solarFlares)

# Extract SpotDistribution into a vector v_spotDistribution

v_spotDistribution<-extractSpotDistribution(solarFlares)

# Create and display summarySunspotClass from v_sunspotClass

summary_SunspotClass<-createSummary(v_sunspotClass)
summary_SunspotClass

# Create and display summarySunspotClass from v_spotSize

summary_SpotSize<-createSummary(v_spotSize)
summary_SpotSize

# Create and display summarySpotDistribution from v_spotDistribution

summary_SpotDistribution<-createSummary(v_spotDistribution)
summary_SpotDistribution