Home > Mobile >  Make selection adjustments in R
Make selection adjustments in R

Time:09-27

I would like to make some selection adjustments. I will present my ideas below:

datas<-dates[dif:12,] - see that I've specified 12 here, as it's the maximum number of days of the datas variable. I didn't want to specify but leave it in an "automatic" way, so that I don't have to keep specifying the value. This is the first question.

Likewise, I would like to make an adjustment to plot(Numbers ~ Days, ylim=c(0.40), date = dates). In ylim=c(0.40), I put 40 because it has a number 34 in datas variable, if I don't specify ylim, the graph goes up to 35. I found the graph ugly, so I increased it to 40. Is there any way to adjust as to this question.

Thank you very much!

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

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(34,31,34,33,33,34,33,36), DR02= c(32,21,16,17,13,12,17,14),DR03= c(39,15,14,13,13,12,11,15),
       DR04 = c(35,32,13,13,16,12,11,19),DR05 = c(35,14,15,13,16,12,11,19),
       DR06 = c(32,14,13,13,15,16,17,18),DR07 = c(32,35,34,34,39,34,37,38),
       DR08 = c(0,0,0,11,12,0,0,0),DR09 = c(0,0,12,11,0,0,0,12),DR010 = c(0,0,0,0,0,0,0,0),DR011 = c(0,0,12,0,0,0,0,0), DR012 = c(0,14,0,0,0,0,0,0)),
  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")


dif <- as.Date(dmda) - as.Date(df1$date1[1])   1

datas<-datas[dif:12,]

plot(Numbers ~ Days, ylim=c(0,40), data = datas)

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

new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))

lines(new.data$Days,predict(model,newdata = new.data))

enter image description here

CodePudding user response:

Extract the column, get the max from it

max(datas$Days, na.rm = TRUE)

Now, do the sequence

dif:max(datas$Days, na.rm = TRUE)
[1]  4  5  6  7  8  9 10 11 12

which can be used for subsetting

datas <- datas[dif:max(datas$Days, na.rm = TRUE),]

Similarly, for the second case, we can get the max of the other column

mxrange <-  range(datas$Numbers) 
mxrange[2] <- mxrange[2] - (mxrange[2] %%10)   10
plot(Numbers ~ Days, ylim= mxrange, data = datas)
  •  Tags:  
  • r
  • Related