4

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.

Tung
  • 26,371
  • 7
  • 91
  • 115
Reza Ahmad
  • 95
  • 2
  • 2
  • 6
  • add a `dput(df)` to your question so we can reproduce your error and actually help as opposed to speculate – Nate Sep 21 '16 at 02:42

1 Answers1

5

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
Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
  • I see. I did try in zoo format and it worked. But because of the further analysis I need to do upon it, I preferred dataframe. Anyway thanks. – Reza Ahmad Sep 21 '16 at 06:42
  • 1
    You're welcome. You might have thought of this already, but you can convert the output from `daily2monthly(z, FUN=sum, dates=1)` back to a dataframe, or insert a placeholder column in your dataframe and remove it after you run `daily2monthly`. If the answer resolved your question, please mark it as accepted. – Weihuang Wong Sep 21 '16 at 06:49