r - Simple Table with dplyr on Sequence Data -


i make simple table

dplyr  

and

summarise 

but can't figure out how ... (even though should quite simple).

i have matrix of sequences. when tabulate

 table(dta)  

i have result want.

 dta             acquaintance                        alone                        child                    notnotnot                      nuclear                         1                            2                           17                           19                          131   nuclear ,     acquaintance  nuclear ,    acquaintance    nuclear ,  acquaintance     nuclear , acquaintance                      partner                         1                            1                            1                           35                            2  

however, can't figure out how same summarise

any suggestion ?

 dta =   structure(c("nuclear", "nuclear", "child", "child", "child",  "acquaintance", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "child", "child",  "child", "alone", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "child", "child", "child",  "child", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "child", "child", "child",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "child", "child",   "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",  "partner", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear",  "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",   "partner", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear , acquaintance",   "nuclear", "nuclear", "nuclear , acquaintance", "nuclear ,  acquaintance",   "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear ,     acquaintance",   "nuclear", "nuclear", "nuclear , acquaintance", "nuclear , acquaintance",   "notnotnot", "nuclear", "nuclear", "nuclear", "nuclear", "nuclear ,    acquaintance",   "nuclear", "nuclear", "nuclear", "nuclear", "notnotnot", "nuclear",    "nuclear", "nuclear", "nuclear", "nuclear , acquaintance",   "nuclear", "nuclear", "nuclear", "nuclear", "notnotnot", "nuclear",  "nuclear", "nuclear", "nuclear", "nuclear , acquaintance",  "nuclear", "nuclear", "nuclear", "nuclear", "notnotnot", "nuclear",  "nuclear", "nuclear", "nuclear", "nuclear , acquaintance",  "nuclear", "nuclear", "nuclear", "nuclear", "notnotnot", "nuclear",  "nuclear", "nuclear", "nuclear", "nuclear , acquaintance",  "nuclear", "nuclear", "child", "nuclear", "notnotnot", "nuclear",  "nuclear", "nuclear", "nuclear", "nuclear , acquaintance",  "nuclear", "nuclear", "child", "alone", "notnotnot", "nuclear" ), .dim = c(10l, 21l), .dimnames = list(c("1", "2", "3", "4",  "5", "6", "7", "8", "9", "10"), c("12:10", "12:20", "12:30",  "12:40", "12:50", "13:00", "13:10", "13:20", "13:30", "13:40",  "13:50", "14:00", "14:10", "14:20", "14:30", "14:40", "14:50",  "15:00", "15:10", "15:20", "15:30"))) 

you have convert data data.frame use dplyr , can desired output:

require(dplyr) # ungrouped data_frame(var = c(dta)) %>%    group_by_("var") %>%    summarise(n()) ##                             var n() ## 1                  acquaintance   1 ## 2                         alone   2 ## 3                         child  17 ## 4                     notnotnot  19 ## 5                       nuclear 131 ## 6  nuclear ,     acquaintance   1 ## 7   nuclear ,    acquaintance   1 ## 8     nuclear ,  acquaintance   1 ## 9      nuclear , acquaintance  35 ## 10                      partner   2 

if want each column seperately, can use tidyr first gather result , spread again.

require(tidyr) # grouped dta %>%    as.data.frame %>%    gather %>%    group_by(key, value) %>%    summarise(n = n()) %>%    spread(key, n) ##                           value 12:10 12:20 12:30 12:40 12:50 13:00 13:10 13:20 13:30 13:40 13:50 14:00 14:10 ## 1                  acquaintance     1    na    na    na    na    na    na    na    na    na    na    na    na ## 2                         alone    na     1    na    na    na    na    na    na    na    na    na    na    na ## 3                         child     3     3     4     3     2    na    na    na    na    na    na    na    na ## 4                     notnotnot     1     1     1     1     1     1     1     1     1     1     1    na    na ## 5                       nuclear     3     3     3     4     5     7     7     7     7     7     7     7     7 ## 6  nuclear ,     acquaintance    na    na    na    na    na    na    na    na    na    na    na    na    na ## 7   nuclear ,    acquaintance    na    na    na    na    na    na    na    na    na    na    na    na    na ## 8     nuclear ,  acquaintance    na    na    na    na    na    na    na    na    na    na    na    na    na ## 9      nuclear , acquaintance     2     2     2     2     2     2     2     2     2     2     2     2     2 ## 10                      partner    na    na    na    na    na    na    na    na    na    na    na     1     1 ## variables not shown: 14:20 (int), 14:30 (int), 14:40 (int), 14:50 (int), 15:00 (int), 15:10 (int), 15:20 (int),  ## 15:30 (int) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -