0
\$\begingroup\$

i try to get the row count for last three column (Kindly refer the below image ).I have done using for loop but the concern is it takes so much of time to execute in my application, and i heard apply is better than for loop or if any other optimized method is there kindly let me know

Here is the Sample Data

table <-structure(list(X = 1:13, type = c("Actual", "Actual", "Actual","Actual", "Actual", "Actual", "Plan", "Plan", "Plan", "Plan","Plan", "Plan", "Plan"), 
                                              Sector = c("AA", "AA", "AA", "AA", "AA","BB", "CC", "BB", "BB", "AA", "AA", "AA", "AA"), 
                                              sub_sector = c("AAA1","AAA1", "AAA1", "AAA1", "AAA1", "BBB3", "CCC1", "BBB2", "BBB1","AAA2", "AAA1", "AAA1", "AAA3"), 
                                              requirement = c("DDD", "DDD2","DDD3", "DDD4", "DDD5", "DDD5", "DDD6", "DDD7", "DDD8", "DDD9","DDD10", "DDD11", "DDD12"), 
                                              service_provider = c("EEE1", "EEE2", "EEE3", "EEE4", "EEE5", "EEE6", "EEE7", NA, NA, NA, "EEE7", "EEE7","EEE7"), 
                                              focus_area = c("Product", "Product", "Product", "Product","Product", 
                                                             "Operations", NA, NA, NA, NA, NA, NA, NA), need_by = c("0000-00-00","0000-00-00", "0000-00-00", "0000-00-00", "30-06-2021", "31-07-2021","30-06-2021", "31-07-2021", "02-08-2021", "30-09-2021", "30-06-2021","30-06-2021", "31-07-2021"), 
                                              contract_start_date = c("01-06-2021","01-06-2021", "01-04-2021", "01-04-2021", "01-04-2021", "14-09-2021","", "", "", "", "", "", ""), 
                                              contract_end_date = c("30-09-2021","30-09-2021", "30-09-2021", "30-09-2021", "30-09-2021", "31-12-2021","", "", "", "", "", "", "")), 
                                              class = "data.frame", row.names = c(NA,-13L))

Here is the sample code that uses the nested for-loop and provide me the require output in the table as shown in the image

expand_collapse_technical <- function(right_table) {
  if(nrow(right_table[right_table$type=="Actual",])==0){
    right_table = right_table[right_table$type=="Plan",]
    right_table = right_table[,c(-1)]
  }else{
    right_table = right_table[right_table$type=="Actual",]
    right_table = right_table[,c(-1)]
  }
right_table = right_table[order(right_table[,1],right_table[,2],
                                  right_table[,4],right_table[,6]),]
  right_table$need_by <- ifelse(right_table$need_by=="0000-00-00",format(as.Date(""),'%d-%m-%Y'),right_table$need_by)
  right_table$contract_start_date <- ifelse(right_table$contract_start_date=="0000-00-00",format(as.Date(""),'%d-%m-%Y'),right_table$contract_start_date)
  right_table$contract_end_date <- ifelse(right_table$contract_end_date=="0000-00-00",format(as.Date(""),'%d-%m-%Y'),right_table$contract_end_date)
  right_table$need_by <- format(as.Date(right_table$need_by),'%d-%m-%Y')
  right_table$contract_start_date <- format(as.Date(right_table$contract_start_date),'%d-%m-%Y')
  right_table$contract_end_date <- format(as.Date(right_table$contract_end_date),'%d-%m-%Y')
  right_table = right_table %>% group_by(sub_sector) %>% mutate(id = row_number())
  right_table = as.data.frame(right_table)
  print(right_table)
  df = data.frame("sub_sector1"=c(""), "sub_sector"=c(""), 
                  "Service Provider"=c(""),"Focus Area"=c(""), 
                  "Need by"=c(""),"Contract Start Date"=c(""),
                  "Contract End Date"=c(""))
  sub_sector_list <- sort(unique(right_table$sub_sector))
  for(i in sub_sector_list){
    df1 = right_table[right_table$sub_sector==i,]
    sub_sector1 = df1$sub_sector[1]
    sub_sector = df1$sub_sector[1]
    provider = ""
    focusarea = ""
    needby = ""
    startdate = ""
    enddate = ""
    mainrow = c(sub_sector1,sub_sector,provider,focusarea,needby,startdate,enddate)
    df = rbind(df,mainrow)
    #provider_list <- df1$service_provider
    id_list <- sort(unique(df1$id))
    for(id in id_list){
      df2 = right_table[right_table$id==id & right_table$sub_sector==i,]
      sub_sector1 = df1$sub_sector[1]
      sub_sector = ""
      provider = df2$service_provider[1]
      focusarea= df2$focus_area[1]
      needby = as.character(df2$need_by[1])
      startdate = as.character(df2$contract_start_date[1])
      enddate = as.character(df2$contract_end_date[1])
      subrow = c(sub_sector1,sub_sector,provider,focusarea,needby,startdate,enddate)
      df = rbind(df,subrow)
    }
  }
  df = df[2:nrow(df),]
  #
  needby_total = sum(!is.na(right_table$need_by))
  start_date_total = sum(!is.na(right_table$contract_start_date))
  end_date_total = sum(!is.na(right_table$contract_end_date))
  df = rbind(df,c("","Total","","",needby_total,start_date_total,end_date_total))
  return(df)
}

The code which i used to create data table

 DT::datatable(expand_collapse_technical(table), 
                  rownames = F,escape = FALSE,
                  selection=list(mode="single",target="row"),
                  options = list(autoWidth = TRUE,pageLength = 50,scrollX = TRUE,dom = 'tp',ordering=F,
                                 columnDefs = list(list(visible=FALSE, targets=c(0,2)),
                                                   list(className = 'dt-left', targets = '_all'),
                                                   list(width = '105px', targets = 2),
                                                   list(width = '78px', targets = 3),
                                                   list(width = '72px', targets = 4))),
                                  class='hover cell-border stripe')

Expected Output

\$\endgroup\$
6
  • 5
    \$\begingroup\$ Welcome to Code Review! What does this code accomplish? Please tell us, and also make that the title of the question. See How to Ask. \$\endgroup\$ Dec 28 '21 at 3:40
  • \$\begingroup\$ i try to get the row count for last three column (Kindly refer the above image ).I have done using for loop but the concern is it takes so much of time to execute in my application i heard apply is better than for loop or if any other optimized method is there kindly let me know the changes @200_success \$\endgroup\$ Dec 28 '21 at 4:19
  • 3
    \$\begingroup\$ Please explain that in the question and the title, instead of in a comment (not everyone is going to read the comments, especially if they see you didn't explain what your code does in the question, they likely won't bother). \$\endgroup\$
    – cliesens
    Dec 28 '21 at 10:27
  • 2
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. \$\endgroup\$
    – Mast
    Dec 28 '21 at 12:10
  • 1
    \$\begingroup\$ Your explanation merely tries to explain what the code does on a functional level. We need a broader view: is this logic for a car rental, does this predict tides, etc. We require context. Please follow the tour, and read "What topics can I ask about here?", "How do I ask a good question?" and "What types of questions should I avoid asking?". \$\endgroup\$
    – BCdotWEB
    Dec 28 '21 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.