R rbind - 参数列数不匹配

标签 r rstudio weather rbind

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

我有一个来自流的天气数据列表,但我认为某些关键天气条件不存在,因此我在下面有此错误 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 (我不能在这里发布它,因为它太长了,我不知道哪里是公开显示 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 中的全部数据导出到我的谷歌驱动器:

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

关于R rbind - 参数列数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41704855/

相关文章:

r - 清除 RStudio 中所有绘图的代码

python - 如何迭代 grib2 文件的纬度经度值?

氧气 : export imported function

使用 data.table 包对 R 中的多个变量进行滚动平均

r - 多边形 shapefile 不在传单 R 中呈现

jQuery + 时钟和天气

java - 带抽屉导航的异步任务

r - 使用 .I 获取错误 : Could not find function "." for only one data. 表

r - 通过SSH在控制台(R,Matlab,Python等)中从vim运行当前行

r - 如何告诉 RStudio 使用包名称自动完成我的函数参数?