我的数据集有377行,我想将每行与数据集中的所有其他行共映射,如果两行完全相等,则打印一条消息。
@model List<EventGalleryViewModel>
@{
ViewBag.Title = "Gellery";
}
<br /><br /><br />
<h2>Gellery</h2>
@foreach (var item in Model)
{
<a href='@Url.Content("~/GalleryImages/" + item.ThumbImage)'>
<img class='thumbnail' src='@Url.Content("~/GalleryImages/" + item.ThumbImage)' />
</a>
}
}
这是情况的简单算法。我对R非常陌生,因此在编写代码时遇到问题,因此该算法可能会帮助您解答。谢谢您的宝贵时间。
答案 0 :(得分:1)
DF <- data.frame(A=c(1,3,2,1,4,3,3),B=c('A','X','B','A','X','X','Y'))
# example input
# > DF
# A B
# 1 1 A
# 2 3 X
# 3 2 B
# 4 1 A
# 5 4 X
# 6 3 X
# 7 3 Y
# find which row indexes have duplicates
rowIndexesWithDupe <- which( duplicated(DF) | rev(duplicated(DF[rev(seq_len(nrow(DF))),])) )
# print
if(length(rowIndexesWithDupe) > 0){
print(paste('Rows:',toString(rowIndexesWithDupe),'have duplicates'))
}else{
print('No duplicates')
}
# output in this case :
[1] "Rows: 1, 2, 4, 6 have duplicates"
编辑:
根据评论,要检查“几乎重复”(例如,差异在<= 5之内),可以使用for循环
(注:我们假设所有值都是数字!):
DF <- data.frame(A=c(1,3,2,10,9,3,3),b=c(10,12,13,16,11,9,8))
# example input
# > DF
# A b
# 1 1 10
# 2 3 12
# 3 2 13
# 4 10 16
# 5 9 11
# 6 3 9
# 7 3 8
toler <- 5
dupes <- integer()
for(i in 1:(nrow(DF)-1)){
row1 <- DF[i,]
for(j in (i+1):nrow(DF)){
row2 <- DF[j,]
if(all(abs(row1-row2) <= toler)){
dupes <- c(dupes,i,j)
}
}
}
dupes <- sort(unique(dupes))
print(paste('Rows:',toString(dupes),'have duplicates within tolerance =',toler))
# output in this case :
[1] "Rows: 1, 2, 3, 6, 7 have duplicates within tolerance = 5"
答案 1 :(得分:1)
您可以使用anyDuplicated
:
DF <- data.frame(c(1,3,2,1,4,3,3), c('A','X','B','A','X','X','Y'))
if (anyDuplicated(DF)) print('Matched')
# [1] "Matched"