{"id":84,"date":"2024-02-20T23:20:30","date_gmt":"2024-02-20T23:20:30","guid":{"rendered":"https:\/\/belenagurtoc.virgen.io\/tech\/?p=84"},"modified":"2024-02-20T23:36:41","modified_gmt":"2024-02-20T23:36:41","slug":"general-r","status":"publish","type":"post","link":"https:\/\/belenagurtoc.virgen.io\/tech\/2024\/02\/20\/general-r\/","title":{"rendered":"General R"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">Check NAs (taken from loop_Ventas)\n\n#check which columns have Nas\nnacols &lt;- function(df) {\ncolnames(df)[unlist(lapply(df, function(x) anyNA(x)))]\n}\nnacols(pre_Vtas_dtll_mov_full)\n\n #check NAs #updated\nNAs_pre_Vtas_dtll_mov_full &lt;-   pre_Vtas_dtll_mov_full %>% filter_all(any_vars( is.na(.)))\n\n data %>% drop_na() \n\n#Replace NAs by Col\nsummarized_285_Ventas$Ventas_Trimestrales[is.na(summarized_285_Ventas$Ventas_Trimestrales)] &lt;-  0.001\n\n\n#Replace NAs all Cols\nTL_Vtas_dtll_mov_Establ_noSufixCostoNo0[is.na(TL_Vtas_dtll_mov_Establ_noSufixCostoNo0)] &lt;- 0\n\n\n#Replace NAs across all Numeric Cols  with a 0\ndat %>% mutate(across(where(is.numeric), function(x) tidyr::replace_na(x, 0)))\n\n\n\n\n#Replace a string\nStockMin_285_max_replaced$StockMax_Trimestral &lt;-  trimws(gsub(\"(2)\", \"3\", StockMin_285_max_replaced$StockMax_Trimestral))\n\n#Assign column names\nnames(pre_dataDespachos_cab_1) &lt;- pre_dataDespachos_cab_1[1,]\n\nnames(dataDespachos_cab)[names(dataDespachos_cab) == \"N\u00ba GENERADO\"] &lt;- \"N_DESPACHO\"\n\n\n#Separate Monofocales from Bi\/Multifocales\n\nlistado_existencias_Series_Mono &lt;- listado_existencias_Series %>% filter(FORMA %in% c(\"ESF\", \"CIL\", \"CMB\"))\n\nlistado_existencias_Series_Multi &lt;- listado_existencias_Series %>% filter(FORMA %in% c(\"BFT\", \"INV\", \"MFC\", \"MFL\")) %>% mutate(MATERIAL=paste(MATERIAL,FORMA))\n\nlistado_existencias_Series_All  &lt;- bind_rows(listado_existencias_Series_Mono, listado_existencias_Series_Multi)\n\n\n#Remove info with #match\nTemp_match0_dtll &lt;- which(tb_bind_ventas_dtll_distinct$ESTABLECIMIENTO==\"Laboratorio A&amp;C\")\n#Re-create variable: Only Distrib\ntb_bind_ventas_dtll_distinct_OnlyDistrib &lt;- tb_bind_ventas_dtll_distinct %>% slice(-Temp_match0_dtll)\n\n\n#Add new field SERIE and fill it considering ESF_CIL and CMB_ADD\n  mutate(SERIE = case_when( \n#esf+ serie 1\n    FORMA == \"ESF\" &amp;  ESF_CIL >= 0 &amp; ESF_CIL &lt;= 4 ~ \"ESFpos_1\",\n(\u2026)\nTRUE ~ \"other\"\n  )\n  )\n\ncount\n\n  only22_PeriodLast_uniques &lt;- filtered_sum_salidas_only22_PeriodLast %>% group_by(CODIGO_ARTICULO) %>% summarise (n = n())\n\nGS_Cols_Kardex_Almacen_class_years2122_filtered4MQ2_22 %>% count(CODIGO_ARTICULO)\n\n\n\n\n#for(i in 1:length(list_285_byTL)) {                    # assign function within loop\n #assign(paste0(\"tb_285_\", i), list_285_byTL[[i]])\n#}\n\n\nlist_285_byTL = split(col_print1_medidas_y_stk_min_285_max_replaced, col_print1_medidas_y_stk_min_285_max_replaced$MATERIAL)\n\n\n\n\n\n#order\n\n\n#wide or long\nwide_exist_adhoc_Category_Stock_Estabs &lt;- li_exist_adhoc_Category_Stock_Estabs %>%\n  pivot_wider(names_from = Establecimiento, values_from = `STOCK ACTUAL`)\n\n\n\n#totals (adorn)\n\n\n\n###############################################################################\n#                               Saving       #\n###############################################################################\n\n#Path\npathToSave &lt;- \"~\/Documents\/@\/CasaDelOptico2023\/Analisis\/scripts\/\"\n\n\n#R file\n#--Stock\n#saveRDS(Ventas_AllEstab_AllCat, file= paste(pathToSave, \"loop_Ventas_detalle_Cab_allDistrib_allCat_01_01_23-29_04_23_v1.rds\", sep = \"\"))\n\n#Excel\n#write.xlsx(Ventas_AllEstab_AllCat, file= paste(pathToSave, \"loop_Ventas_detalle_cat_allDistrib_allCat_01_01_23-29_04_23_v1.xlsx\", sep = \"\"), colNames=TRUE, rowNames = FALSE,showNA = TRUE)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Check NAs (taken from loop_Ventas) #check which columns have Nas nacols &lt;- function(df) { colnames(df)[unlist(lapply(df, function(x) anyNA(x)))] } nacols(pre_Vtas_dtll_mov_full) #check NAs #updated NAs_pre_Vtas_dtll_mov_full &lt;- pre_Vtas_dtll_mov_full %>% filter_all(any_vars( is.na(.))) data %>% drop_na() #Replace NAs by Col summarized_285_Ventas$Ventas_Trimestrales[is.na(summarized_285_Ventas$Ventas_Trimestrales)] &lt;- 0.001 #Replace NAs all Cols TL_Vtas_dtll_mov_Establ_noSufixCostoNo0[is.na(TL_Vtas_dtll_mov_Establ_noSufixCostoNo0)] &lt;- 0 #Replace NAs across all Numeric Cols with a 0 dat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":1,"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/posts\/84\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/belenagurtoc.virgen.io\/tech\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}