how to solve this error: attempt to set 'rownames' on an object with no dimensions
how to solve this error: attempt to set 'rownames' on an object with no dimensions
我有一个包含每日流量的 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
有人可以帮助我解决这个问题。提前致谢。
首先尝试将您的 df
转换为 zoo
对象:
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
daily2monthly
的 data.frame
方法仅在您有不止一列数据时才有效;在你的情况下,你只有一个,这就是为什么函数 returns 那个错误信息。假设我们还有一列数据:
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
我有一个包含每日流量的 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
有人可以帮助我解决这个问题。提前致谢。
首先尝试将您的 df
转换为 zoo
对象:
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
daily2monthly
的 data.frame
方法仅在您有不止一列数据时才有效;在你的情况下,你只有一个,这就是为什么函数 returns 那个错误信息。假设我们还有一列数据:
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