-1

I am getting data from CSV file using read.csv but I want particular date data from file So I am using filter function and it gives data but the problem with retrieval data is supposed I give 01/01/2014 Output data is both 01/01/2015 and 01/01/2014 because of this problem I am unable to create correct scatter plot. So plz suggest how can I correct this problem My R code is below.

library(sos)
library(dplyr)

da <- read.csv(file.choose(),header = TRUE)

df <- da %>% filter(as.Date(DATE) == as.Date("01/01/2014"))

plot(df$Time,df$Nox,xlab ="Time",ylab = "NOx",
     Main="data Related City",las=1)

please suggest...:)

My data is as follows:

 DATE       Time    Nox
 01/01/2014 8:00    81.9
 01/01/2014 9:00    83.2
 01/01/2014 10:00   111.3
 01/01/2014 11:00   100.6
 01/01/2014 12:00   96.5
 01/01/2014 13:00   98.7
 01/01/2014 14:00   93.8
 01/01/2014 15:00   96.6
 01/01/2014 16:00   98.6
 01/01/2014 17:00   116.9
 01/01/2014 18:00   126.7
 01/01/2014 19:00   188.3
 01/01/2014 20:00   127.1
 01/01/2014 21:00   97.1
 01/01/2014 22:00   109.7
 01/01/2014 23:00   104.1
 01/01/2015 8:00    118.8
 01/01/2015 9:00    154.1
 01/01/2015 10:00   152.4
 01/01/2015 11:00   144.5
 01/01/2015 12:00   133.3
 01/01/2015 13:00   168.9
 01/01/2015 14:00   215.9
 01/01/2015 15:00   139.4
 01/01/2015 16:00   170.3
 01/01/2015 17:00   184.7
 01/01/2015 18:00   
 01/01/2015 19:00   
 01/01/2015 20:00   151.7
 01/01/2015 21:00   138.5
 01/01/2015 22:00   80.6
 01/01/2015 23:00   85
DTYK
  • 1,062
  • 1
  • 7
  • 31
  • 1
    have you checked the Date format in your csv file is similar to one you are trying to do it using filter. Please have a look at that first. Also can you provide us reproducible example using dput() – Hunaidkhan Sep 14 '18 at 06:39
  • ya sir my csv date format is also 01/01/2014 but it gives both year values – user2889556 Sep 14 '18 at 06:43
  • 2
    Please share your data and the code you're working on so others can help. See more here [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tung Sep 14 '18 at 06:52
  • A quick way to see what's going wrong is to run `as.Date(da$DATE)[1:10]` you can see that the formatting is all wrong and requires `format = "%d/%m/%Y"` as described by Paul below – Sarah Sep 14 '18 at 07:15

1 Answers1

1

Try adding a format to your as.Date():

library(dplyr)
dat <- data_frame(date = c("1/1/2014", "1/1/2015"))
dat$date <- dat$date %>%
  as.Date(format = "%d/%m/%Y")
dat %>%
  filter(date == as.Date("1/1/2014", format = "%d/%m/%Y"))

Gives

# A tibble: 1 x 1
  date      
  <date>    
1 2014-01-01

UPDATE with supplied data (saved as .csv) via. tidyverse

library(dplyr)
library(ggplot2)
library(readr)

dat <- read_csv("dat.csv")
dat$DATE <- dat$DATE %>%
  as.Date(format = "%d/%m/%Y")

dat %>%
  filter(DATE == as.Date("1/1/2014", format = "%d/%m/%Y")) %>%
  ggplot(aes(x = Time, y = Nox)) +
  geom_point() +
  labs(title = "Data Related City", x = "Time", y = "NOx")
Paul
  • 2,715
  • 1
  • 9
  • 27