How to split one table into many smaller tables

Hi,
I have got a table that consists of a few 2 by 2 tables inside it. How can I get all those tables as dataframes out of it ?
thank you

temp <- structure(list(Existence = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("not_exists", 
"exists"), class = "factor"), Group = structure(c(2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("Group_1", 
"Group_2"), class = "factor"), Sex = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("F",  
"M"), class = "factor"), Side = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("right", 
"left"), class = "factor"), r1 = c(35, 77, 28, 70, 37, 75, 24, 
74, 23, 27, 21, 31, 20, 30, 18, 34), r2 = c(21, 91, 7, 91, 17, 
95, 8, 90, 18, 32, 5, 47, 19, 31, 7, 45), r3 = c(62, 50, 41, 
57, 47, 65, 35, 63, 28, 22, 29, 23, 28, 22, 31, 21)), class = "data.frame", row.names = c(NA, 
-16L), variable.labels = structure(character(0), .Names = character(0)), codepage = 65001L)


(totsum1 <- sum(temp$r1+temp$r2+temp$r3))

(temp2 <- pivot_longer(data=temp,
                      cols=c(r1,r2,r3)))

temp3 <- temp2 %>%
   uncount(weights = .$value) %>% 
   select(-value) %>% table()
temp2 %>% 
group_by(Sex,Side,name) %>% 
group_map(~xtabs(value~Existence+Group,data=.) %>%
 as.data.frame() %>% 
pivot_wider(names_from = "Group",values_from="Freq"))
1 Like

Thank you very much indeed, @nirgrahamuk

When I tried subset a table I received an error that $ is not for atomic vectors. Whatever it means.

What was your code for that ?

Here you are:

library(tidyverse)

temp <- structure(list(Existence = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("not_exists", 
"exists"), class = "factor"), Group = structure(c(2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("Group_1", 
"Group_2"), class = "factor"), Sex = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("F",  
"M"), class = "factor"), Side = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("right", 
"left"), class = "factor"), r1 = c(35, 77, 28, 70, 37, 75, 24, 
74, 23, 27, 21, 31, 20, 30, 18, 34), r2 = c(21, 91, 7, 91, 17, 
95, 8, 90, 18, 32, 5, 47, 19, 31, 7, 45), r3 = c(62, 50, 41, 
57, 47, 65, 35, 63, 28, 22, 29, 23, 28, 22, 31, 21)), class = "data.frame", row.names = c(NA, 
-16L), variable.labels = structure(character(0), .Names = character(0)), codepage = 65001L)

(temp2 <- pivot_longer(data=temp,
                      cols=c(r1,r2,r3)))
#> # A tibble: 48 x 6
#>    Existence  Group   Sex   Side  name  value
#>    <fct>      <fct>   <fct> <fct> <chr> <dbl>
#>  1 exists     Group_2 F     right r1       35
#>  2 exists     Group_2 F     right r2       21
#>  3 exists     Group_2 F     right r3       62
#>  4 not_exists Group_2 F     right r1       77
#>  5 not_exists Group_2 F     right r2       91
#>  6 not_exists Group_2 F     right r3       50
#>  7 exists     Group_1 F     right r1       28
#>  8 exists     Group_1 F     right r2        7
#>  9 exists     Group_1 F     right r3       41
#> 10 not_exists Group_1 F     right r1       70
#> # ... with 38 more rows


temp3 <- temp2 %>%
   uncount(weights = .$value) %>% 
   select(-value) %>% table()

temp3 %>% dimnames()
#> $Existence
#> [1] "not_exists" "exists"    
#> 
#> $Group
#> [1] "Group_1" "Group_2"
#> 
#> $Sex
#> [1] "F" "M"
#> 
#> $Side
#> [1] "right" "left" 
#> 
#> $name
#> [1] "r1" "r2" "r3"

temp3$Existence
#> Error in temp3$Existence: $ operator is invalid for atomic vectors

temp3$Group
#> Error in temp3$Group: $ operator is invalid for atomic vectors

temp3$Sex
#> Error in temp3$Sex: $ operator is invalid for atomic vectors

temp3$Side
#> Error in temp3$Side: $ operator is invalid for atomic vectors

temp3$name
#> Error in temp3$name: $ operator is invalid for atomic vectors

Created on 2022-01-28 by the reprex package (v2.0.1)

Ok, well that's because table () creates an array and you simply don't access it with $ like you would a data.frame, but i thought that's why you asked for the table info in dataframe structure, which is my solution that you accepted. Why aren't you working from that but reverting to the original table() code you were trying to improve upon?

I like your solution and my next step was to extract all dataframes from your solution which I did.
I tried to understand why $ is not good for tables and you explained that table was an array.
But I like that layout of the table() where everything is nicely laid out with detailed descriptions about all subgroups:

As the next step I have done this:

temp4 <- temp2 %>% 
group_by(Sex,Side,name) %>% 
group_map(~xtabs(value~Existence+Group,data=.) %>%
 as.data.frame() %>% 
pivot_wider(names_from = "Group",values_from="Freq"))

for (i in 1:length(temp4)) {
  assign(paste0("temp4_", i), as.data.frame(temp4[[i]]))
}

and now I have got all little 2 by 2 dataframes to calculate Odds Ratio I hope.

And for the sake of clarity with your solution Nir would it be possible to name those dataframes like in here:

as when I use your solution it looks like this:

which is OK, but I lost description to which groups this data belongs to ?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.