giacomoV giacomoV - 3 months ago 17
R Question

R dplyr count in a list

I can't figure how to properly

count
(or
table
) in a
list
object.

So my
list
is named
v
and it has 3 elements, is it possible to avoid doing the following ?

library(dplyr)

v[[1]] %>% count(group, variable, value)
v[[2]] %>% count(group, variable, value)
v[[3]] %>% count(group, variable, value)


My problem using
table
is that I have to group several variables (here
group
,
variable
and
value
).

Solution with
table
, which is not great output

ft = function(x) table(x)
v %>% lapply(ft)


Output wanted

[[1]] # list 1
group variable value n
1 V1 FALSE 1
1 V1 TRUE 2
1 V2 FALSE 2
1 V2 TRUE 1
...

[[2]] # list 2
group variable value n
1 V1 FALSE 1
1 V1 TRUE 2
1 V2 FALSE 3
1 V2 TRUE 3
...

# and so on #


Generate the data

library(purrr)
library(reshape2)

v = list('vector', 3)

for(i in 1:3){
dt = replicate(10, rbernoulli(9)) %>% as.data.frame()
dt$group = rep(1:3, 3)
v[[i]] = dt %>% melt(id.vars = 'group')
}

Answer

We can use the purrr package

library(dplyr)
library(purrr)
v %>%
   map(~count(., group, variable, value))
#[[1]]
#Source: local data frame [56 x 4]
#Groups: group, variable [?]

#   group variable value     n
#   <int>   <fctr> <lgl> <int>
#1      1       V1 FALSE     1
#2      1       V1  TRUE     2
#3      1       V2 FALSE     2
#4      1       V2  TRUE     1
#5      1       V3 FALSE     3
#6      1       V4 FALSE     1
#7      1       V4  TRUE     2
#8      1       V5  TRUE     3
#9      1       V6 FALSE     2
#10     1       V6  TRUE     1
#..   ...      ...   ...   ...

#[[2]]
#Source: local data frame [49 x 4]
#Groups: group, variable [?]

#   group variable value     n
#   <int>   <fctr> <lgl> <int>
#1      1       V1 FALSE     3
#2      1       V2 FALSE     2
#3      1       V2  TRUE     1
#4      1       V3 FALSE     2
#5      1       V3  TRUE     1
#6      1       V4  TRUE     3
#7      1       V5  TRUE     3
#8      1       V6 FALSE     1
#9      1       V6  TRUE     2
#10     1       V7 FALSE     1
#..   ...      ...   ...   ...

#[[3]]
#Source: local data frame [54 x 4]
#Groups: group, variable [?]

#   group variable value     n
#   <int>   <fctr> <lgl> <int>
#1      1       V1  TRUE     3
#2      1       V2 FALSE     2
#3      1       V2  TRUE     1
#4      1       V3 FALSE     2
#5      1       V3  TRUE     1
#6      1       V4  TRUE     3
#7      1       V5 FALSE     1
#8      1       V5  TRUE     2
#9      1       V6 FALSE     2
#10     1       V6  TRUE     1
#..   ...      ...   ...   ...

NOTE: Works in purrr_0.2.1 and purrr_0.2.2