简体   繁体   中英

how to solve this error: attempt to set 'rownames' on an object with no dimensions

I have a csv file with daily streamflow. I need to combine the daily values into monthly. I am trying to use the "daily2monthly" function of "hydroTSM" package. sample data from the 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

I used the following commands:

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)

And it gives the following error message:

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

Could someone help me regarding this. Thanks in advance.

Try converting your df into a zoo object first:

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 

The data.frame method for daily2monthly only works if you have more than one column of data; in your case, you only have one, which is why the function returns that error message. Let's say we had one more column of data:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM