Home > Mobile >  How to insert new column in my generated table
How to insert new column in my generated table

Time:10-12

Could you help me to insert the column Category in my generated table? That way I can know specifically the coef for each day and category.

    library(purrr)
    library(dplyr)
    library(tidyverse)
    library(lubridate)
    
    df1 <- structure(
      list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
           date2 = c("2021-06-30","2021-06-30","2021-07-01","2021-07-01"),
           Category = c("FDE","ABC","FDE","ABC"),
           Week= c("Wednesday","Wednesday","Friday","Friday"),
           DR1 = c(4,1,6,3),
           DR01 = c(4,1,4,3), DR02= c(4,2,6,2),DR03= c(9,5,4,7),
           DR04 = c(5,4,3,2),DR05 = c(5,4,5,4),
           DR06 = c(2,4,3,2),DR07 = c(2,5,4,4),
           DR08 = c(3,4,5,4),DR09 = c(2,3,4,4)),
      class = "data.frame", row.names = c(NA, -4L))
    
    dates <- subset(df1, date2 > date1, select = date2)$date2
    
    map_dfr(dates, ~ {
      
      datas <- df1 %>%
        filter(date2 == ymd(.x)) %>%
        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")
      mod <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 47,b2 = 0), data = datas)
      tibble(dates = .x, coef = coef(mod)[2])
    
      }) %>%
      mutate(dates = format(ymd(dates), "%d/%m/%Y"))
# A tibble: 4 x 2
  dates       coef
  <chr>      <dbl>
1 30/06/2021  7.89
2 30/06/2021  7.89
3 01/07/2021  7.95
4 01/07/2021  7.95

In this case, it looks like this:

  dates       Category  coef
  <chr>                 <dbl>
1 30/06/2021    FDE     7.89
2 30/06/2021    ABC     7.89
3 01/07/2021    FDE     7.95
4 01/07/2021    ABC     7.95

CodePudding user response:

You can use bind_cols() with a dataset containing the Category column

##Select the Category column as a subset
categories <- subset(df1, date2 > date1, select =  Category)

map_dfr(dates, ~ {

  datas <- df1 %>%
    filter(date2 == ymd(.x)) %>%
    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")
  mod <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 47,b2 = 0), data = datas)
  tibble(dates = .x[[1]], coef = coef(mod)[2])
  
}) %>% bind_cols(categories)  %>% #add the categories to the tibble
  mutate(dates = format(ymd(dates), "%d/%m/%Y"))
  •  Tags:  
  • r
  • Related