Search code examples
rdplyraddition

How to add rows and columns to an outcome got by table() function


I have the following dataset:

I have been trying to build a contingency table, by using the following code:

library(readr)
library(tidyverse)
library(magrittr)

data1 %>% 
  select(blockLabel, trial_resp.corr, participant) %>% 
  group_by(blockLabel, trial_resp.corr, participant) %$% 
  with(., table(blockLabel, trial_resp.corr, participant)) 


, , participant = pilot01

                        trial_resp.corr
blockLabel                0  1
  auditory_only           0 12
  bimodal_focus_auditory  1 71
  bimodal_focus_visual    3 69
  divided                74 70
  visual_only             0 12

, , participant = pilot02

                        trial_resp.corr
blockLabel                0  1
  auditory_only           0 12
  bimodal_focus_auditory  1 71
  bimodal_focus_visual    2 70
  divided                77 67
  visual_only            11  1

, , participant = pilot03

                        trial_resp.corr
blockLabel                0  1
  auditory_only           1 11
  bimodal_focus_auditory  1 71
  bimodal_focus_visual    3 69
  divided                75 69
  visual_only             0 12

What I would like to do is to add a further column with values (under 1 and 0) converted in percentage and a final with the Total.

I do not know whether it is possible, but if not, please suggest some iterative ways (do.call(), apply(), map(), for loop) to do this if the first way is not possible.

I have used the following solution but I do not how to move on

data1%>%  select(blockLabel, trial_resp.corr, participant) %>% 
  group_by(blockLabel, trial_resp.corr, participant) %$% 
  as.data.frame(with(., table(blockLabel, trial_resp.corr, participant))) %>% 
  mutate(freq = Freq / sum(Freq)) %>% 
  group_split(participant, trial_resp.corr)

[[1]]
# A tibble: 5 x 5
  blockLabel             trial_resp.corr participant  Freq    freq
  <fct>                  <fct>           <fct>       <int>   <dbl>
1 auditory_only          0               pilot01         0 0      
2 bimodal_focus_auditory 0               pilot01         1 0.00107
3 bimodal_focus_visual   0               pilot01         3 0.00321
4 divided                0               pilot01        74 0.0791 
5 visual_only            0               pilot01         0 0      

[[2]]
# A tibble: 5 x 5
  blockLabel             trial_resp.corr participant  Freq   freq
  <fct>                  <fct>           <fct>       <int>  <dbl>
1 auditory_only          1               pilot01        12 0.0128
2 bimodal_focus_auditory 1               pilot01        71 0.0759
3 bimodal_focus_visual   1               pilot01        69 0.0737
4 divided                1               pilot01        70 0.0748
5 visual_only            1               pilot01        12 0.0128

[[3]]
# A tibble: 5 x 5
  blockLabel             trial_resp.corr participant  Freq    freq
  <fct>                  <fct>           <fct>       <int>   <dbl>
1 auditory_only          0               pilot02         0 0      
2 bimodal_focus_auditory 0               pilot02         1 0.00107
3 bimodal_focus_visual   0               pilot02         2 0.00214
4 divided                0               pilot02        77 0.0823 
5 visual_only            0               pilot02        11 0.0118 

[[4]]
# A tibble: 5 x 5
  blockLabel             trial_resp.corr participant  Freq    freq
  <fct>                  <fct>           <fct>       <int>   <dbl>
1 auditory_only          1               pilot02        12 0.0128 
2 bimodal_focus_auditory 1               pilot02        71 0.0759 
3 bimodal_focus_visual   1               pilot02        70 0.0748 
4 divided                1               pilot02        67 0.0716 
5 visual_only            1               pilot02         1 0.00107

[[5]]
# A tibble: 5 x 5
  blockLabel             trial_resp.corr participant  Freq    freq
  <fct>                  <fct>           <fct>       <int>   <dbl>
1 auditory_only          0               pilot03         1 0.00107
2 bimodal_focus_auditory 0               pilot03         1 0.00107
3 bimodal_focus_visual   0               pilot03         3 0.00321
4 divided                0               pilot03        75 0.0801 
5 visual_only            0               pilot03         0 0      

[[6]]
# A tibble: 5 x 5
  blockLabel             trial_resp.corr participant  Freq   freq
  <fct>                  <fct>           <fct>       <int>  <dbl>
1 auditory_only          1               pilot03        11 0.0118
2 bimodal_focus_auditory 1               pilot03        71 0.0759
3 bimodal_focus_visual   1               pilot03        69 0.0737
4 divided                1               pilot03        69 0.0737
5 visual_only            1               pilot03        12 0.0128

Thanks


Solution

  • We could collapse with as.data.frame

    data1 %>% 
      select(blockLabel, trial_resp.corr, participant) %>% 
      group_by(blockLabel, trial_resp.corr, participant) %$% 
      with(., table(blockLabel, trial_resp.corr, participant))  %>%
      as.data.frame