gpt4 book ai didi

R rbind - 参数列数不匹配

转载 作者:行者123 更新时间:2023-12-04 13:04:17 27 4
gpt4 key购买 nike

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

我有一个来自流的天气数据列表,但我认为某些关键天气条件不存在,因此我在下面有此错误 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com