访问XDF文件中的行

时间:2019-02-14 10:34:12

标签: r microsoft-r

我正在解决https://github.com/MicrosoftLearning/20773_Analyzing-Big-Data-with-Microsoft-R/blob/master/Instructions/20773A_LAB_AK_04.md中的练习2。代码创建了计算时区时间的函数。我不明白为什么可以通过函数内部的departureYear <- dataList[[departureYearVarIndex]][i]访问XDF文件中的变量和行,但是如果我尝试仅通过子集文件:

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)
)

flightDelayDataSubset[[1]][1]它告诉我“错误...该S4类不可子集化”。

如何访问XDF文件中的元素?为什么它在功能中起作用,但对于现有文件却不能手动起作用?我可能会问错问题,因为我不了解此功能的工作原理。函数参数dataList作为XDF文件列的向量

给出
transformFunc = standardizeTimes,
        transformVars = c("Year", "Month", "DayofMonth", "DepTime", "ActualElapsedTime", "OriginTimeZone")

。在函数中,它被视为列表[[]]而不是dataList[[arrivalTimeVarIndex]]。我完全感到困惑。问题可能是,此函数如何知道哪个参数与

中的哪个参数相关
transformVars = c("Year", "Month", "DayofMonth", "DepTime", "ActualElapsedTime", "OriginTimeZone")?

函数如下:

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")
)

1 个答案:

答案 0 :(得分:1)

我的dplyrXdf软件包使您可以对Xdf文件进行索引和子集化,就像它们是数据帧一样。具体来说,它定义了[[$方法来从文件中提取列(但由于实现问题,所以没有[