Simple Table with dplyr on Sequence Data

拥有回忆 提交于 2019-12-07 00:58:30

You just have to convert your data to a data.frame to use dplyr and then you can easily get your 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 and     acquaintance   1
## 7   nuclear and    acquaintance   1
## 8     nuclear and  acquaintance   1
## 9      nuclear and acquaintance  35
## 10                      partner   2

If you want to do this for each column seperately, you can use tidyr to first gather the result and then spread it 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 and     acquaintance    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 7   nuclear and    acquaintance    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 8     nuclear and  acquaintance    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
## 9      nuclear and 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)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!