#kreiranje tabela osnovni na?in
##radimo na WHO

list.files()
##  [1] "1.instal-vectori-i-df.html"       "1.instal vectori i df.R"         
##  [3] "2.indeksiranje.html"              "2.indeksiranje.R"                
##  [5] "3.Na.html"                        "3.Na.R"                          
##  [7] "4.tables-in-R.html"               "4.tables-in-R.R"                 
##  [9] "4.tables-in-R.spin.R"             "4.tables-in-R.spin.Rmd"          
## [11] "4.tables in R.R"                  "importing big files in.R"        
## [13] "indeksiranje - brisati.R"         "install-old-package.html"        
## [15] "install old package.R"            "List of packages-for classes.txt"
## [17] "mydf.csv"                         "report.xlsx"                     
## [19] "rsconnect"                        "startanje-u-R.html"              
## [21] "startanje u R.R"                  "tabels-in-Rmd-5pcgs.docx"        
## [23] "tabels-in-Rmd-5pcgs.html"         "tabels-in-Rmd-5pcgs.knit.md"     
## [25] "tabels in Rmd 5pcgs.Rmd"          "Tokyo_updated.xlsx"              
## [27] "update R.txt"                     "Uvod u R i R studio_R-figure"    
## [29] "WHO.csv"                          "WHO_labelled.csv"                
## [31] "Zadaci lesson 1.docx"             "Zadaci nakon lesson 1.R"
Tokyo_updated <- readxl::read_xlsx("Tokyo_updated.xlsx") #mozemo importovati i na ovaj nacin nismo pozvali library () nego smo prisli funkciji preko dvije tacke
## New names:
## • `` -> `...13`
str(Tokyo_updated)
## tibble [1,884 × 13] (S3: tbl_df/tbl/data.frame)
##  $ Store      : chr [1:1884] "Tokyo" "Tokyo" "Tokyo" "Tokyo" ...
##  $ Brand      : chr [1:1884] "Asics" "Asics" "Asics" "Asics" ...
##  $ Type       : chr [1:1884] "WB1820" "Kayano Single Tab" "WB1820" "WB2585" ...
##  $ Gender     : chr [1:1884] "Female" "Unisex" "Female" "Female" ...
##  $ Size       : chr [1:1884] "42" "42-44" "37" "39" ...
##  $ Color      : chr [1:1884] "Blue" "Blue" "Pink" "Black" ...
##  $ Category   : chr [1:1884] "Pants" "Socks" "Pants" "Pants" ...
##  $ Sales Price: num [1:1884] 89 25 89 99 89 ...
##  $ Date       : POSIXct[1:1884], format: "2015-07-15" "2015-07-15" ...
##  $ Time       : POSIXct[1:1884], format: "1899-12-31 07:32:09" "1899-12-31 07:33:36" ...
##  $ Loyalty    : chr [1:1884] "41842" "46176" "---" "40444" ...
##  $ Month      : num [1:1884] 7 7 7 7 7 7 7 7 7 7 ...
##  $ ...13      : num [1:1884] 29 29 29 29 29 29 29 29 29 29 ...
table (Tokyo_updated$Brand)
## 
## Adidas  Asics   Nike 
##    213   1058    613
table (Tokyo_updated$Store)
## 
## Tokyo 
##  1884
#ako zelimo dva elementa u tabeli onda nam obje varijable moraju biti kategoricke. 
table(Tokyo_updated$Brand, Tokyo_updated$Gender)
##         
##          Female Male Unisex
##   Adidas     87  126      0
##   Asics     570  288    200
##   Nike      253   49    311
tabela <- table (Tokyo_updated$Brand, Tokyo_updated$Gender)
tabela
##         
##          Female Male Unisex
##   Adidas     87  126      0
##   Asics     570  288    200
##   Nike      253   49    311
prop.table(tabela,1)
##         
##              Female       Male     Unisex
##   Adidas 0.40845070 0.59154930 0.00000000
##   Asics  0.53875236 0.27221172 0.18903592
##   Nike   0.41272431 0.07993475 0.50734095
prop.table(tabela,2)
##         
##             Female      Male    Unisex
##   Adidas 0.0956044 0.2721382 0.0000000
##   Asics  0.6263736 0.6220302 0.3913894
##   Nike   0.2780220 0.1058315 0.6086106
#install.packages ("tableone")----
#install.packages ("purrr")
#install.packages ("glue")

library(tableone) 

CreateTableOne(data=Tokyo_updated, vars = c("Brand","Sales Price" ), strata = c("Gender")) # izbacuje mean i sd u odnosu na stratu
##                          Stratified by Gender
##                           Female         Male           Unisex        p     
##   n                          910            463           511               
##   Brand (%)                                                           <0.001
##      Adidas                   87 ( 9.6)     126 (27.2)      0 ( 0.0)        
##      Asics                   570 (62.6)     288 (62.2)    200 (39.1)        
##      Nike                    253 (27.8)      49 (10.6)    311 (60.9)        
##   Sales Price (mean (SD)) 100.31 (44.07) 125.89 (39.76) 21.10 (2.10)  <0.001
##                          Stratified by Gender
##                           test
##   n                           
##   Brand (%)                   
##      Adidas                   
##      Asics                    
##      Nike                     
##   Sales Price (mean (SD))
#da ova formula profunkcionise moramo instalirati pakete glue i purrr

#objasnjenje za mean i sd - tzv apply funkcije
tapply(Tokyo_updated$`Sales Price`, Tokyo_updated$Gender, mean)
##    Female      Male    Unisex 
## 100.30777 125.89417  21.10162
tapply(Tokyo_updated$`Sales Price`, Tokyo_updated$Gender, sd)
##    Female      Male    Unisex 
## 44.065790 39.761840  2.096324
#drugi paket

#install.packages("descr") ---- 

library(descr)
crosstab(Tokyo_updated$Brand,Tokyo_updated$Category, prop.r=T, plot=T, digits=1)

##    Cell Contents 
## |-------------------------|
## |                   Count | 
## |             Row Percent | 
## |-------------------------|
## 
## =============================================================
##                        Tokyo_updated$Category
## Tokyo_updated$Brand      Bra   Pants     Shoe   Socks   Total
## -------------------------------------------------------------
## Adidas                    0       0      213       0     213 
##                         0.0%    0.0%   100.0%    0.0%   11.3%
## -------------------------------------------------------------
## Asics                     0     683       82     293    1058 
##                         0.0%   64.6%     7.8%   27.7%   56.2%
## -------------------------------------------------------------
## Nike                    218       0       84     311     613 
##                        35.6%    0.0%    13.7%   50.7%   32.5%
## -------------------------------------------------------------
## Total                   218     683      379     604    1884 
## =============================================================
crosstab(Tokyo_updated$Category,Tokyo_updated$Brand, prop.r=T, plot=T, digits=1) #zamjeniti mjesta za druge proporcije

##    Cell Contents 
## |-------------------------|
## |                   Count | 
## |             Row Percent | 
## |-------------------------|
## 
## ==========================================================
##                           Tokyo_updated$Brand
## Tokyo_updated$Category    Adidas    Asics     Nike   Total
## ----------------------------------------------------------
## Bra                           0        0      218     218 
##                             0.0%     0.0%   100.0%   11.6%
## ----------------------------------------------------------
## Pants                         0      683        0     683 
##                             0.0%   100.0%     0.0%   36.3%
## ----------------------------------------------------------
## Shoe                        213       82       84     379 
##                            56.2%    21.6%    22.2%   20.1%
## ----------------------------------------------------------
## Socks                         0      293      311     604 
##                             0.0%    48.5%    51.5%   32.1%
## ----------------------------------------------------------
## Total                       213     1058      613    1884 
## ==========================================================