0

I'm working on a data analysis project for hydrological modelling data. I've exported the results to .csv format and integrated into R as data frame (Out_1). Afterwards I selected some variables I need as you can see below.

Out_1 <- read.csv("Outlets_1.csv",header = TRUE)
Out_1s <- select(Out_1,SUB,YEAR,MON,AREAkm2,EVAPcms,FLOW_OUTcms,SED_OUTtons,YYYYMM)
str(Out_1s)
'data.frame':   480 obs. of  8 variables:
 $ SUB        : int  19 19 19 19 19 19 19 19 19 19 ...
 $ YEAR       : int  1983 1983 1983 1983 1983 1983 1983 1983 1983 1983 ...
 $ MON        : int  1 2 3 4 5 6 7 8 9 10 ...
 $ AREAkm2    : int  1025 1025 1025 1025 1025 1025 1025 1025 1025 1025 ...
 $ EVAPcms    : num  0.00601 0.00928 0.01696 0.01764 0.02615 ...
 $ FLOW_OUTcms: num  2.31 2.84 3.16 18.49 34.42 ...
 $ SED_OUTtons: num  215 308 416 3994 11440 ...
 $ YYYYMM     : int  198301 198302 198303 198304 198305 198306 198307 198308 198309 198310 ...


typeof(Out_1s$YEAR)
[1] "integer"
typeof(Out_1s$MON)
[1] "integer"
typeof(Out_1s$YYYYMM)
[1] "integer"

What I try to do exactly is to create graphical summaries with ggplot2 based on either combining the Out_1s.YEAR and Out_1s.MON columns or to identify the Out_1s.YYYYMM variable as YYYY-MM or MM-YYYY.

Out_1s$Date <- NA
typeof(Out_1s$Date)
[1] "character"    

Out_1s$Date <- paste(Out_1s$YEAR,Out_1s$MON, sep = "-")
as.Date.character(Out_1s$Date, "%Y-%m")

graph1 <- ggplot(Out_1s, aes(Date, FLOW_OUTcms ))
graph1 + geom_line()

And the result which is not actually what was expected.

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50
GeoBar
  • 37
  • 7
  • 1
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 14 '18 at 16:11
  • 1
    This might help https://stackoverflow.com/a/53197221/786542 – Tung Nov 14 '18 at 16:12

1 Answers1

1

Two problems here.

  • First, a Date object is a year, month and day. To fix add a "01" to the paste statement.

    Out_1s$Date <- paste(Out_1s$YEAR,Out_1s$MON, "01", sep = "-")
    

    In your case since the date did not include a day, the as.Date function would return a series of NAs

  • Second, is the need to reassign the result from the as.Date function back to the original column.

    Out_1s$Date <- as.Date.character(Out_1s$Date, "%Y-%m-%d")
    
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • problem solved didn't know that is obligatory to include just the %d to make the system work! Thank you so much !!! – GeoBar Nov 14 '18 at 16:38