有很多方法可以做到这一点,其中一些是上面提出的。我通常使用dplyr
版本来发现和删除重复/不好的情况。根据您的目标,以下是各种输出的示例。
library(dplyr)
# example with one bad case
dt = data.frame(Name = c("david","davud","John","John","megan"),
ID = c(1,1,2,3,3), stringsAsFactors = F)
# spot names with more than 1 unique IDs
dt %>%
group_by(Name) %>%
summarise(NumIDs = n_distinct(ID)) %>%
filter(NumIDs > 1)
# # A tibble: 1 x 2
# Name NumIDs
# <chr> <int>
# 1 John 2
# spot names with more than 1 unique IDs and the actual IDs
dt %>%
group_by(Name) %>%
mutate(NumIDs = n_distinct(ID)) %>%
filter(NumIDs > 1) %>%
ungroup()
# # A tibble: 2 x 3
# Name ID NumIDs
# <chr> <dbl> <int>
# 1 John 2 2
# 2 John 3 2
# spot names with more than 1 unique IDs and the actual IDs - alternative
dt %>%
group_by(Name) %>%
mutate(NumIDs = n_distinct(ID)) %>%
filter(NumIDs > 1) %>%
group_by(Name, NumIDs) %>%
summarise(IDs = paste0(ID, collapse=",")) %>%
ungroup()
# # A tibble: 1 x 3
# Name NumIDs IDs
# <chr> <int> <chr>
# 1 John 2 2,3
独特的名称,添加ID,然后把它合并 – Wen
我将无法使用唯一的(名称),以原始数据集,因为这样的长度是不同的后合并? – Rachel
您将可以合并。合并是基于公共值的查找功能。与Access或vlookup中的dlookup和Excel或Calc中的hlookup类似。 –