R rbind - 参数的列数不匹配

R rbind - numbers of columns of arguments do not match

如果某些列名不存在,如何忽略数据集?

我有一个来自流的天气数据列表,但我认为某些关键天气条件不存在,因此我在下面出现此错误 rbind:

Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match 

我的代码:

weatherDf <- data.frame()

for(i in weatherData) {
    # Get the airport code.
    airport <- i$airport

    # Get the date.
    date <- as.POSIXct(as.numeric(as.character(i$timestamp))/1000, origin="1970-01-01", tz="UTC-1")

    # Get the data in dailysummary only.
    dailySummary <- i$dailysummary

    weatherDf <- rbind(weatherDf, ldply(
        list(dailySummary),
        function(x) c(airport, format(as.Date(date), "%Y-%m-%d"), x[["meanwindspdi"]], x[["meanwdird"]], x[["meantempm"]], x[["humidity"]])
    ))
}

那么我怎样才能确保数据中存在以下这些关键条件:

meanwindspdi
meanwdird
meantempm
humidity

如果其中任何个没有退出,那么忽略这一堆。可能吗?

编辑:

weatherData 的内容在 jsfiddle 中(我不能 post 在这里,因为它太长了,我不知道在哪里是公开显示 R 数据的最佳位置.. .)

编辑 2:

当我尝试将数据导出到 txt 时出现一些错误:

> write.table(weatherData,"/home/teelou/Desktop/data/data.txt",sep="\t",row.names=FALSE)
Error in data.frame(date = list(pretty = "January 1, 1970", year = "1970",  : 
  arguments imply differing number of rows: 1, 0

这是什么意思?好像数据有些错误...

编辑 3:

我已将 .RData 中的全部数据导出到我的 google 驱动器:

https://drive.google.com/file/d/0B_w5RSQMxtRSbjdQYWJMX3pfWXM/view?usp=sharing

如果你使用RStudio,那么你可以只导入数据。

编辑 4:

target_names <- c("meanwindspdi", "meanwdird", "meantempm", "humidity")

# If it has data then loop it.
if (!is.null(weatherData)) {
    # Initialize a data frame.
    weatherDf <- data.frame()

    for(i in weatherData) {
        if (!all(target_names %in% names(i)))
            next

        # Get the airport code.
        airport <- i$airport

        # Get the date.
        date <- as.POSIXct(as.numeric(as.character(i$timestamp))/1000, origin="1970-01-01", tz="UTC-1")

        # Get the data in dailysummary only.
        dailySummary <- i$dailysummary

        weatherDf <- rbind(weatherDf, ldply(
            list(dailySummary),
            function(x) c(airport, format(as.Date(date), "%Y-%m-%d"), x[["meanwindspdi"]], x[["meanwdird"]], x[["meantempm"]], x[["humidity"]])
        ))
    }

    # Rename column names.
    colnames(weatherDf) <- c("airport", "key_date", "ws", "wd", "tempi", 'humidity')

    # Convert certain columns weatherDf type to numberic.
    columns <-c("ws", "wd", "tempi", "humidity")
    weatherDf[, columns] <- lapply(columns, function(x) as.numeric(weatherDf[[x]]))
}

检查 weatherDf:

> View(weatherDf)
Error in .subset2(x, i, exact = exact) : subscript out of bounds

您可以使用next跳过循环的当前迭代并转到下一个迭代:

target_names <- c("meanwindspdi", "meanwdird", "meantempm", "humidity")

for(i in weatherData) {
  if (!all(target_names %in% names(i)))
    next
  # continue with loop...