gpt4 book ai didi

r - 如何解决此错误 : attempt to set 'rownames' on an object with no dimensions

转载 作者:行者123 更新时间:2023-12-04 10:31:32 28 4
gpt4 key购买 nike

我有一个带有每日流的 csv 文件。我需要将每日值合并为每月。我正在尝试使用“hydroTSM”包的“daily2monthly”功能。
来自 BRPT2.csv 的样本数据:

_date,_time,_value,_flag
10/2/1959,0:00:00,0,2
10/3/1959,0:00:00,0,2
10/4/1959,0:00:00,1540,2
10/5/1959,0:00:00,16100,2
10/6/1959,0:00:00,6680,2
10/7/1959,0:00:00,3100,2
10/8/1959,0:00:00,2060,2

我使用了以下命令:
qme<- read.csv(file = "BRPT2.csv",header = T,sep = ",") #read in csv file
date<- as.Date(qme$X_date,format("%Y-%m-%d")) #convert date column to date format from factor
flow<- qme[,3]
flow_2<-replace(flow,flow==-999,0) #replace the missing values (-999) with 0
df<- data.frame()
df<- rbind(df,data.frame(date,flow_2,stringsAsFactors = FALSE))
daily2monthly(df,FUN=sum,dates=1)

它给出了以下错误消息:

Error in rownames<-(*tmp*, value = c("Oct-1959", "Nov-1959", "Dec-1959", : attempt to set 'rownames' on an object with no dimensions



有人可以帮我解决这个问题。提前致谢。

最佳答案

尝试转换您的 dfzoo对象第一:

z <- zoo(df[, -1], df[, 1])
daily2monthly(z, FUN=sum, dates=1)
# 1959-10-01 1959-11-01 1959-12-01 1960-01-01 1960-02-01 1960-03-01 1960-04-01
# 31440.0 411.9 1199.3 4373.0 1466.0 1904.0 741.0
data.frame daily2monthly 的方法仅当您有不止一列数据时才有效;在您的情况下,您只有一个,这就是该函数返回该错误消息的原因。假设我们还有一列数据:
df$station_3 <- sample(1:1000, nrow(df))
daily2monthly(df, FUN=sum, dates=1)
# flow_2 station_3
# Oct-1959 31440.0 132423
# Nov-1959 411.9 149305
# Dec-1959 1199.3 157622
# Jan-1960 4373.0 176413
# Feb-1960 1466.0 143373
# Mar-1960 1904.0 166102
# Apr-1960 741.0 141861

关于r - 如何解决此错误 : attempt to set 'rownames' on an object with no dimensions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39606580/

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