将唯一/单词单词计入新列

时间:2018-02-05 11:18:20

标签: r count unique

我有一个数据集,其中包含每行中包含一组国家/地区的列。有时国家不止一次重复,我想计算下面数据集每行中唯一国家的数量:

> class(address_countries2$address_countries)
[1] "character"

> head(address_countries2)
                    address_countries
1                         China China
2                   China China China
3                         China China
4                         China China
5 China China China China China China
6                China China Uk China

所需的输出将是这样的新列:

                    address_countries n_countries
1                         China China           1
2                   China China China           1
3                         China China           1
4                         China China           1
5 China China China China China China           1
6                China China Uk China           2

这段代码给出了每行内的单词数量:

address_countries2 <- address_countries2 %>% 
  select(address_countries) %>% 
  mutate(n_countries = str_count(address_countries, boundary("word")))


> head(address_countries2)
                    address_countries n_countries
1                         China China           2
2                   China China China           3
3                         China China           2
4                         China China           2
5 China China China China China China           6
6                China China Uk China           4

我尝试使用str_count()添加unique()以及n_distinct()和distinct()但是我收到此错误:

Error in mutate_impl(.data, dots) : 
  Column `n_countries` must be length 34760 (the number of rows) or one, not 39

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

试试这个:

您的data.frame

address_countries2<-data.frame(address_countries=c("Chian","China China","China UK"))

统计国家数量:

list_country<-strsplit(as.character(address_countries2$address_countries)," ")
list_country
[[1]]
[1] "Chian"

[[2]]
[1] "China" "China"

[[3]]
[1] "China" "UK"  

添加“n_countries”列

address_countries2$n_countries<-unlist(lapply(lapply(list_country, unique),length))

输出

address_countries2
        address_countries n_countries
        1             Chian           1
        2       China China           1
        3          China UK           2

答案 1 :(得分:1)

您可以将address_countries拆分为列表,然后使用n_distinct

library(purrr)
library(dplyr)
library(stringr)

df %>% 
  mutate(n_countries = map_int(address_countries, ~
                                 .x %>% 
                                 str_trim %>% 
                                 str_split(" ") %>% 
                                 unlist() %>% 
                                 n_distinct))

map_intaddress_countries的每个元素之后应用跟随的函数并输出整数
str_trim删除向量开头和结尾的空格 str_split使用" "作为分割模式分割向量 unlist将str_split的结果转换为向量
n_distinct计算结果向量的唯一元素

数据

df <- tibble(address_countries = c("China China", "China China China", "China China", 
                                   "China China", "China China China China China China", 
                                   "China China Uk China"))

答案 2 :(得分:1)

这可以给你你想要的东西:

ac$n_countries <- lengths(lapply(strsplit(ac$countries, split = ' '), unique))

结果:

> ac
    countries n_countries
1       Chian           1
2 China China           1
3    China UK           2

数据:

ac <- data.frame(countries = c("Chian","China China","China UK"), stringsAsFactors = FALSE)