Question about table function

I have a question regarding the table function. As you can see in the screenshot, I have several rounds in my sample. For each round 0, 1, 2, 3 and 4 are possible values. Therefore I want to find out the number per value over several rounds. I have done this for round_01:
table(data$round_01).

But how can i do it that i have the number of each value over 30 rounds and not only for round_01?Bildschirmfoto 2020-03-05 um 15.44.26

Hi @Rproblems. I guess you want to run table for each column. You may use sapply or map on your data in data frame format. The output will be a list of 30 results.

library(tidyverse)

data <- sample(0:4, 600, TRUE) %>%
  matrix(ncol = 30, byrow = TRUE) %>%
  `colnames<-`(paste0("round_", str_pad(1:30, 2, pad = "0"))) %>%
  {cbind(participants = 1:20, .)} %>%
  as.data.frame()

data
#>    participants round_01 round_02 round_03 round_04 round_05 round_06 round_07
#> 1             1        1        0        3        2        1        0        4
#> 2             2        1        3        4        4        2        3        0
#> 3             3        1        2        4        3        1        2        0
#> 4             4        2        2        0        2        4        4        2
#> 5             5        1        1        3        2        3        0        4
#> 6             6        0        1        0        3        3        4        0
#> 7             7        0        1        3        0        3        0        2
#> 8             8        2        3        3        3        1        1        0
#> 9             9        1        4        4        3        0        2        2
#> 10           10        0        4        3        3        1        3        3
#> 11           11        3        2        4        1        4        3        4
#> 12           12        2        4        3        2        2        3        4
#> 13           13        2        1        1        0        0        4        1
#> 14           14        0        3        3        3        1        2        0
#> 15           15        4        2        0        0        3        2        0
#> 16           16        2        1        2        4        3        3        0
#> 17           17        3        2        2        0        0        4        2
#> 18           18        1        3        3        3        2        0        1
#> 19           19        4        0        0        0        4        0        4
#> 20           20        4        0        3        0        2        3        4
#>    round_08 round_09 round_10 round_11 round_12 round_13 round_14 round_15
#> 1         1        3        1        1        1        0        1        2
#> 2         0        3        3        4        0        1        3        3
#> 3         4        4        1        0        4        0        0        0
#> 4         4        2        2        0        3        0        1        2
#> 5         3        2        0        4        1        1        0        3
#> 6         3        4        4        0        3        2        0        1
#> 7         4        3        3        4        3        0        3        1
#> 8         0        3        0        3        2        0        2        4
#> 9         3        3        4        0        4        0        2        1
#> 10        2        3        1        2        4        4        1        4
#> 11        3        0        2        2        3        4        2        2
#> 12        3        4        0        3        0        1        1        3
#> 13        2        4        4        3        4        3        3        2
#> 14        2        2        2        4        0        3        4        1
#> 15        0        1        2        3        4        1        3        0
#> 16        4        3        4        3        0        0        3        0
#> 17        0        2        3        2        2        1        3        3
#> 18        3        0        3        4        2        1        2        4
#> 19        0        4        1        0        3        2        1        4
#> 20        4        1        3        4        3        0        4        2
#>    round_16 round_17 round_18 round_19 round_20 round_21 round_22 round_23
#> 1         1        3        1        0        3        3        3        2
#> 2         2        1        2        3        0        1        0        3
#> 3         0        1        4        0        4        3        3        2
#> 4         4        4        2        1        1        1        3        2
#> 5         2        1        4        4        4        4        4        4
#> 6         2        1        4        0        4        2        4        4
#> 7         1        3        2        1        2        4        1        0
#> 8         0        0        3        3        2        2        3        4
#> 9         3        3        2        4        2        4        3        2
#> 10        3        0        4        4        0        0        1        1
#> 11        0        2        3        4        4        1        0        3
#> 12        0        1        3        1        4        3        4        0
#> 13        3        3        2        3        2        2        4        4
#> 14        2        4        4        2        0        3        3        1
#> 15        4        4        4        4        1        4        2        0
#> 16        3        0        2        3        4        0        1        2
#> 17        2        2        2        1        4        2        4        1
#> 18        1        2        3        4        4        3        4        2
#> 19        3        3        4        3        4        0        2        2
#> 20        3        3        1        2        4        3        1        4
#>    round_24 round_25 round_26 round_27 round_28 round_29 round_30
#> 1         1        0        1        4        1        4        4
#> 2         2        0        2        3        0        2        4
#> 3         0        0        0        3        4        2        0
#> 4         0        2        0        2        4        0        1
#> 5         2        0        4        1        4        2        1
#> 6         4        0        2        4        4        3        4
#> 7         1        1        2        4        0        1        0
#> 8         3        2        3        4        3        2        0
#> 9         3        3        1        0        0        4        2
#> 10        0        4        3        0        0        4        0
#> 11        0        4        0        1        1        1        2
#> 12        4        4        2        1        0        3        0
#> 13        2        0        1        3        1        1        4
#> 14        4        1        1        4        0        0        0
#> 15        1        2        0        0        2        1        3
#> 16        0        4        0        2        2        0        2
#> 17        1        0        3        4        1        0        4
#> 18        4        0        1        3        0        4        0
#> 19        0        1        3        0        4        4        3
#> 20        0        1        4        1        0        3        2
  
sapply(data[, -1], function(x){table(x)})
#> $round_01
#> x
#> 0 1 2 3 4 
#> 4 6 5 2 3 
#> 
#> $round_02
#> x
#> 0 1 2 3 4 
#> 3 5 5 4 3 
#> 
#> $round_03
#> x
#> 0 1 2 3 4 
#> 4 1 2 9 4 
#> 
#> $round_04
#> x
#> 0 1 2 3 4 
#> 6 1 4 7 2 
#> 
#> $round_05
#> x
#> 0 1 2 3 4 
#> 3 5 4 5 3 
#> 
#> $round_06
#> x
#> 0 1 2 3 4 
#> 5 1 4 6 4 
#> 
#> $round_07
#> x
#> 0 1 2 3 4 
#> 7 2 4 1 6 
#> 
#> $round_08
#> x
#> 0 1 2 3 4 
#> 5 1 3 6 5 
#> 
#> $round_09
#> x
#> 0 1 2 3 4 
#> 2 2 4 7 5 
#> 
#> $round_10
#> x
#> 0 1 2 3 4 
#> 3 4 4 5 4 
#> 
#> $round_11
#> x
#> 0 1 2 3 4 
#> 5 1 3 5 6 
#> 
#> $round_12
#> x
#> 0 1 2 3 4 
#> 4 2 3 6 5 
#> 
#> $round_13
#> x
#> 0 1 2 3 4 
#> 8 6 2 2 2 
#> 
#> $round_14
#> x
#> 0 1 2 3 4 
#> 3 5 4 6 2 
#> 
#> $round_15
#> x
#> 0 1 2 3 4 
#> 3 4 5 4 4 
#> 
#> $round_16
#> x
#> 0 1 2 3 4 
#> 4 3 5 6 2 
#> 
#> $round_17
#> x
#> 0 1 2 3 4 
#> 3 5 3 6 3 
#> 
#> $round_18
#> x
#> 1 2 3 4 
#> 2 7 4 7 
#> 
#> $round_19
#> x
#> 0 1 2 3 4 
#> 3 4 2 5 6 
#> 
#> $round_20
#> x
#>  0  1  2  3  4 
#>  3  2  4  1 10 
#> 
#> $round_21
#> x
#> 0 1 2 3 4 
#> 3 3 4 6 4 
#> 
#> $round_22
#> x
#> 0 1 2 3 4 
#> 2 4 2 6 6 
#> 
#> $round_23
#> x
#> 0 1 2 3 4 
#> 3 3 7 2 5 
#> 
#> $round_24
#> x
#> 0 1 2 3 4 
#> 7 4 3 2 4 
#> 
#> $round_25
#> x
#> 0 1 2 3 4 
#> 8 4 3 1 4 
#> 
#> $round_26
#> x
#> 0 1 2 3 4 
#> 5 5 4 4 2 
#> 
#> $round_27
#> x
#> 0 1 2 3 4 
#> 4 4 2 4 6 
#> 
#> $round_28
#> x
#> 0 1 2 3 4 
#> 8 4 2 1 5 
#> 
#> $round_29
#> x
#> 0 1 2 3 4 
#> 4 4 4 3 5 
#> 
#> $round_30
#> x
#> 0 1 2 3 4 
#> 7 2 4 2 5

Created on 2020-03-06 by the reprex package (v0.3.0)

1 Like

Thank you very much for your reply.

I would like to have only one table at the end, where the values of all rounds are added together.
How can i do that?

Hello, while I found the table function useful for adhoc / interactive analysis, I usually don't like it in my workflows, i.e. scripts that do processes, because I find it annoying to work with the table output type.
Here is a version of code that is an alternative, avoiding 'table' but giving you counts for each round, for each type of round.

library(tidyverse)
cols<- 30
data <- sample(0:4, 600, TRUE) %>%
  matrix(ncol = cols, byrow = TRUE) %>%
  `colnames<-`(paste0("round_", str_pad(1:cols, 2, pad = "0"))) %>%
  {cbind(participants = 1:20, .)} %>%
  as.data.frame()

rounds_only <- data %>% select(starts_with("round"))

mapped_names <- names(rounds_only)

mapped  <-  map2(.x = rounds_only,
                 .y = mapped_names,
                 .f = ~enframe(x = .x,value = "round_base" ,name=NULL) %>% group_by_all() %>% count(name = paste0(.y,"_n")))


round_base <- list(tibble(round_base=0:4))

mapped2<- append(mapped,round_base,after=0)

final_result <- reduce(mapped2,.f=left_join)

@Rproblems. If you want all results in one data frame, you can do it as follow.

library(tidyverse)

data <- sample(0:4, 600, TRUE) %>%
  matrix(ncol = 30, byrow = TRUE) %>%
  `colnames<-`(paste0("round_", str_pad(1:30, 2, pad = "0"))) %>%
  {cbind(participants = 1:20, .)} %>%
  as.data.frame()

data[, -1] %>%
  gather(round, value) %>%
  group_by(round) %>%
  summarise(list(as.data.frame(table(value)))) %>%
  unnest() %>%
  spread(value, Freq, fill = 0)
#> Warning: `cols` is now required.
#> Please use `cols = c(`list(as.data.frame(table(value)))`)`
#> # A tibble: 30 x 6
#>    round      `0`   `1`   `2`   `3`   `4`
#>    <chr>    <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 round_01     5     4     5     3     3
#>  2 round_02     2     3     7     6     2
#>  3 round_03     8     0     4     3     5
#>  4 round_04     5     4     1     6     4
#>  5 round_05     5     2     5     3     5
#>  6 round_06     2     6     7     3     2
#>  7 round_07     4     3     3     5     5
#>  8 round_08     3     4     4     5     4
#>  9 round_09     6     8     2     3     1
#> 10 round_10     6     3     8     1     2
#> # … with 20 more rows

Created on 2020-03-06 by the reprex package (v0.3.0)

Thanks for the answers. But i would like to have it as follow:

#> round 0 1 2 3 4
#> round_01 to round_30 (rounds1-30) 12 17 28 10 2

Can somebody help me?

transposed_result <- t(final_result)

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