【发布时间】:2018-07-19 10:04:34
【问题描述】:
我想计算一个国家单独列出的次数,以及与其他国家一起列出的次数。
这是我的数据集的一部分:
address_countries2
name_countries n_countries
China 1
China 1
Usa 1
Usa 1
China France 2
China France 2
India 1
India 1
Jordan Germany 2
我已经使用下面的代码来提取每个国家出现的次数。
publication_countries <- address_countries2 %>%
select(name_countries, n_countries) %>%
unnest_tokens(word, name_countries) %>%
group_by(word) %>%
summarise(TP = n())
head(publication_countries)
# A tibble: 6 x 2
word TP
<chr> <int>
1 China 4
2 Usa 2
3 France 2
4 India 2
5 Jordan 1
6 Germany 1
我想创建一个新列,其中包含一个国家/地区单独列出的行数,以及第二个列,其中包含一个国家/地区与其他国家/地区一起列出的次数。
期望的输出 像这样的:
head(publication_countries)
# A tibble: 6 x 2
word TP single_times with_other_countries
<chr> <int> <int> <int>
1 China 4 2 2
2 Usa 2 2 0
3 France 2 0 2
4 India 2 2 0
5 Jordan 1 0 1
6 Germany 1 0 1
从link 我看到了一种可能的方法来用条件进行总结,但是,在我的情况下,我需要使用不同于 sum() 的东西,因为我的条件对象是字符形式的(列字)。
summarise(TP = n() , IP = count(word[n_countries=="1"]))
但我得到这个错误:
Error in summarise_impl(.data, dots) :
Evaluation error: no applicable method for 'groups' applied to an object of class "character"
任何帮助将不胜感激:)
非常感谢
【问题讨论】: