Home > Enterprise >  How to adjust error when generating a graph in R software
How to adjust error when generating a graph in R software

Time:09-23

Could you help me solve the following problem: I'm not able to generate the graph for the df2 dataset. As you can see for df1 it generates normally, however if you test for df2 it gives an error.

Thank you very much!

library(dplyr)
library(lubridate)
library(tidyverse)

#dataset df1
df1 <- structure(
  list(date1 = c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28",
                 "2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-04-02","2021-04-03","2021-04-08","2021-04-09","2021-04-10","2021-07-01","2021-07-02","2021-07-03"),
       Week= c("Friday","Saturday","Thursday","Friday","Saturday","Thursday","Friday","Monday"),
       DR01 = c(4,1,4,3,3,4,3,6), DR02= c(4,2,6,7,3,2,7,4),DR03= c(9,5,4,3,3,2,1,5),
       DR04 = c(5,4,3,3,6,2,1,9),DR05 = c(5,4,5,3,6,2,1,9),
       DR06 = c(2,4,3,3,5,6,7,8),DR07 = c(2,5,4,4,9,4,7,8)),
  class = "data.frame", row.names = c(NA, -8L))

#Generate graph

dmda<-"2021-07-01"

datas<-df1 %>%
  filter(date2 == ymd(dmda)) %>%
  summarize(across(starts_with("DR"), sum)) %>%
  pivot_longer(everything(), names_pattern = "DR(. )", values_to = "val") %>%
  mutate(name = as.numeric(name))
colnames(datas)<-c("Days","Numbers")
attach(datas)
plot(Numbers ~ Days, ylim=c(0,20))

model <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 47,b2 = 0))

new.data <- data.frame(Days = seq(min(Days),max(Days),len = 45))
lines(new.data$Days,predict(model,newdata = new.data))

enter image description here

df2 <- structure(
  list(date1 = c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28",
                 "2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-04-02","2021-04-03","2021-04-08","2021-04-09","2021-04-10","2021-07-01","2021-07-02","2021-07-03"),
       Week= c("Friday","Saturday","Thursday","Friday","Saturday","Thursday","Friday","Monday"),
       DR01_DR01_PV = c(4,1,4,3,3,4,3,6), DR02_DR02_PV= c(4,2,6,7,3,2,7,4),DR03_DR03_PV= c(9,5,4,3,3,2,1,5), DR04_DR04_PV = c(5,4,3,3,6,2,1,9),DR05_DR05_PV = c(5,4,5,3,6,2,1,9),
       DR06_DR06_PV = c(2,4,3,3,5,6,7,8),DR07_DR07_PV = c(2,5,4,4,9,4,7,8)),
  class = "data.frame", row.names = c(NA, -8L))


#Generate graph

dmda<-"2021-07-01"
   
 datas<-df2 %>%
    filter(date2 == ymd(dmda)) %>%
    summarize(across(starts_with("DR"), sum)) %>%
    pivot_longer(everything(), names_pattern = "DR(. )", values_to = "val") %>%
    mutate(name = as.numeric(name))
Warning message:
Problem with `mutate()` column `name`.
i `name = as.numeric(name)`.
i NAs introduced by coercion 
colnames(datas)<-c("Days","Numbers")

CodePudding user response:

It is just a warning message, and the warning is because the column 'name' is having characters other than digits. We may use parse_number instead

library(dplyr)
library(tidyr)
df2 %>% 
   filter(date2 == ymd(dmda)) %>% 
   summarize(across(starts_with("DR"), sum)) %>% 
   pivot_longer(everything(), names_pattern = "DR(. )", 
     values_to = "val") %>%
   mutate(name = readr::parse_number(name))

-output

# A tibble: 7 x 2
   name   val
  <dbl> <dbl>
1     1     4
2     2     2
3     3     2
4     4     2
5     5     2
6     6     6
7     7     4

i.e. after the pivot_longer, it returns the whole column name because the one captured is . ie. one or more any character . refers to any character in regex

> df2 %>% 
     filter(date2 == ymd(dmda)) %>% 
     summarize(across(starts_with("DR"), sum)) %>% 
     pivot_longer(everything(), names_pattern = "DR(. )", 
       values_to = "val")
# A tibble: 7 x 2
  name         val
  <chr>      <dbl>
1 01_DR01_P      4
2 02_DR02_PV     2
3 03_DR03_PV     2
4 04_DR04_PV     2
5 05_DR05_PV     2
6 06_DR06_PV     6
7 07_DR07_PV     4

Instead, if we change the (. ) to (\\d ), then it matches more specifically to the digits part

df2 %>% 
   filter(date2 == ymd(dmda)) %>% 
   summarize(across(starts_with("DR"), sum)) %>% 
   pivot_longer(everything(), names_pattern = "DR(\\d )", 
     values_to = "val")
# A tibble: 7 x 2
  name    val
  <chr> <dbl>
1 01        4
2 02        2
3 03        2
4 04        2
5 05        2
6 06        6
7 07        4
  •  Tags:  
  • r
  • Related