有很多方法可以做到這一點,其中一些是上面提出的。我通常使用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類似。 –