I am solving the exercise 2 from https://github.com/MicrosoftLearning/20773_Analyzing-Big-Data-with-Microsoft-R/blob/master/Instructions/20773A_LAB_AK_04.md. The code creates the function which calculates the zone time etc. I do not understand why one can access variables and rows in the XDF File by for example departureYear <- dataList[[departureYearVarIndex]][i] inside the function, but if I try to do the same just from the subset file:
rxOptions(reportProgress = 1)
flightDelayDataSubsetFile <- "\\\\LON-RSVR\\Data\\flightDelayDataSubset.xdf"
flightDelayDataSubset <- rxDataStep(inData = mergedFlightDelayData,
outFile = flightDelayDataSubsetFile, overwrite = TRUE,
rowSelection = rbinom(.rxNumRows, size = 1, prob = 0.005)
)
like e.g. flightDelayDataSubset[[1]][1] it tells me "Error...this S4 class is not subsettable".
How to access the elements in XDF file? Why does it work in the function, but not manually for an existing file? I probably ask the wrong question because I do not understand how this function works. The functions argument dataList is given as a vector of XDF File Columns
transformFunc = standardizeTimes,
transformVars = c("Year", "Month", "DayofMonth", "DepTime", "ActualElapsedTime", "OriginTimeZone")
. In the function it is treated as a list [[]] instead dataList[[arrivalTimeVarIndex]]. I am totally confused how it works. The question is probably, how this function knows which argument relates to which argument in the
transformVars = c("Year", "Month", "DayofMonth", "DepTime", "ActualElapsedTime", "OriginTimeZone")?
The function looks like this:
standardizeTimes <- function (dataList) {
# Check to see whether this is a test chunk
if (.rxIsTestChunk) {
return(dataList)
}
# Create a new vector for holding the standardized departure time
# and add it to the list of variable values
departureTimeVarIndex <- length(dataList) + 1
dataList[[departureTimeVarIndex]] <- rep(as.numeric(NA), times = .rxNumRows)
names(dataList)[departureTimeVarIndex] <- "StandardizedDepartureTime"
# Do the same for standardized arrival time
arrivalTimeVarIndex <- length(dataList) + 1
dataList[[arrivalTimeVarIndex]] <- rep(as.numeric(NA), times = .rxNumRows)
names(dataList)[arrivalTimeVarIndex] <- "StandardizedArrivalTime"
departureYearVarIndex <- 1
departureMonthVarIndex <- 2
departureDayVarIndex <- 3
departureTimeStringVarIndex <- 4
elapsedTimeVarIndex <- 5
departureTimezoneVarIndex <- 6
# Iterate through the rows and add the standardized arrival and departure times
for (i in 1:.rxNumRows) {
# Get the local departure time details
departureYear <- dataList[[departureYearVarIndex]][i]
departureMonth <- dataList[[departureMonthVarIndex]][i]
departureDay <- dataList[[departureDayVarIndex]][i]
departureHour <- trunc(as.numeric(dataList[[departureTimeStringVarIndex]][i]) / 100)
departureMinute <- as.numeric(dataList[[departureTimeStringVarIndex]][i]) %% 100
departureTimeZone <- dataList[[departureTimezoneVarIndex]][i]
# Construct the departure date and time, including timezone
departureDateTimeString <- paste(departureYear, "-", departureMonth, "-", departureDay, " ", departureHour, ":", departureMinute, sep="")
departureDateTime <- as.POSIXct(departureDateTimeString, tz = departureTimeZone)
# Convert to UTC and store it
standardizedDepartureDateTime <- format(departureDateTime, tz="UTC")
dataList[[departureTimeVarIndex]][i] <- standardizedDepartureDateTime
# Calculate the arrival date and time
# Do this by adding the elapsed time to the departure time
# The elapsed time is stored as the number of minutes (an integer)
elapsedTime = dataList[[5]][i]
standardizedArrivalDateTime <- format(as.POSIXct(standardizedDepartureDateTime) + minutes(elapsedTime))
# Store it
dataList[[arrivalTimeVarIndex]][i] <- standardizedArrivalDateTime
}
# Return the data including the new variables
return(dataList)
}
flightDelayDataTimeZonesFile <- "\\\\LON-RSVR\\Data\\flightDelayDataTimezones.xdf"
flightDelayDataTimeZones <- rxDataStep(inData = flightDelayDataSubset,
outFile = flightDelayDataTimeZonesFile, overwrite = TRUE,
transformFunc = standardizeTimes,
transformVars = c("Year", "Month", "DayofMonth", "DepTime", "ActualElapsedTime", "OriginTimeZone"),
transformPackages = c("lubridate")
)