Home > front end >  How to add a column with proportion in a dplyr code but on object created with table() function
How to add a column with proportion in a dplyr code but on object created with table() function

Time:10-10

I have the following dataset:

dput(head(data, 50))
structure(list(Treatment = c("A", "A", "B", "A", "A", "A", "A", 
"B", "B", "B", "B", "A", "A", "B", "A", "B", "A", "B", "A", "A", 
"A", "A", "B", "B", "B", "A", "B", "B", "A", "B", "B", "B", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "A", "B", "A", 
"B", "A", "A", "B"), Death = c(1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 
1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 
0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1), `Last observation (days)` = c(276, 
212, 154, 222, 33, 299, 344, 180, 49, 324, 74, 66, 196, 269, 
353, 332, 302, 211, 69, 55, 338, 103, 108, 7, 199, 64, 10, 236, 
82, 242, 34, 239, 197, 315, 243, 5, 126, 44, 260, 363, 246, 193, 
190, 151, 279, 215, 142, 183, 328, 119), `Age (years)` = c(92.64, 
15.68, 10.39, 66.43, 79.59, 74.24, 77.06, 31.06, 11.28, 52.65, 
16.66, 13.01, 42.91, 63.8, 9.99, 1.92, 33.52, 8.68, 61.97, 28.99, 
86.73, 16.96, 5.8, 51.27, 21.28, 36.08, 26.12, 64.53, 52.99, 
7.17, 42.37, 57.63, 83.48, 67.67, 1.12, 23.16, 81.61, 6.47, 72.69, 
29.15, 73.69, 60.3, 9.21, 18.6, 34.73, 24.31, 0.37, 22.06, 9.89, 
30.78), `Age (years)_cat` = c("old", "young", "young", "old", 
"old", "old", "old", "young", "young", "old", "young", "young", 
"young", "old", "young", "young", "young", "young", "old", "young", 
"old", "young", "young", "old", "young", "young", "young", "old", 
"old", "young", "young", "old", "old", "old", "young", "young", 
"old", "young", "old", "young", "old", "old", "young", "young", 
"young", "young", "young", "young", "young", "young")), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

I have to create a table with the table() function, as I did here:

data %>%
  mutate(Death = ifelse(Death == 0, 'No Death', 'Death'))%$%
  as.data.frame(with(., table(Death, Treatment))) 

and then add a column with the corresponding frequencies. I have used the code as follows for this purpose:

data %>%
  mutate(Death = ifelse(Death == 0, 'No Death', 'Death')) %>%
  group_by(Treatment, Death) %>% 
  summarize(n = n()) %>% 
  mutate(freq = n/sum(n)) 

but I am wondering whether it is possible to reach the same results by using the code above.

Thanks

CodePudding user response:

Using prop.table you could do:

library(magrittr)
library(dplyr)

data %>%
  mutate(Death = ifelse(Death == 0, "No Death", "Death")) %$%
  prop.table(table(Death, Treatment), 2) %>%
  as.data.frame()
#>      Death Treatment      Freq
#> 1    Death         A 0.6333333
#> 2 No Death         A 0.3666667
#> 3    Death         B 0.6000000
#> 4 No Death         B 0.4000000

UPDATE Another option would be to add a group_by mutate to compute the percentages:

data %>%
  mutate(Death = ifelse(Death == 0, "No Death", "Death")) %$%
  as.data.frame(with(., table(Death, Treatment))) %>%
  group_by(Treatment) %>% 
  mutate(freq = Freq / sum(Freq))
#> # A tibble: 4 × 4
#> # Groups:   Treatment [2]
#>   Death    Treatment  Freq  freq
#>   <fct>    <fct>     <int> <dbl>
#> 1 Death    A            19 0.633
#> 2 No Death A            11 0.367
#> 3 Death    B            12 0.6  
#> 4 No Death B             8 0.4
  • Related