在 R 中使用组合打破平局

发布于 2025-01-11 12:12:16 字数 651 浏览 0 评论 0原文

我想使用以下逻辑打破多数/复数投票的平局:

  1. 我有两个向量 vName 和 v2。 v1 和 v2 都是字符向量,有 5 行。
  2. 如果出现以下情况,则出现平局: a- 如果 vName 中的每一行在 v2 中都有不同的对应值 b- 如果 vName 中的两行在 v2 中具有相同的对应值,另外两行具有相同的值(与前两行选取的值不同),并且最后一行在 v2 中具有不同的对应值,该值不同于前一行(这两行和另外两行)选取的两个不同值

例如: 比如说,我们有一个向量:

vName<-c("x1","x2","x3","x4","x5") 

v5<-c("John","Abraham","Isaac","Abraham","Isaac")
v4<-c("John","Abraham","Isaac","Isaac","Isaac")

第二行和第四行选择了值“Abraham”,第三行和第五行选择了值“Isaac”,而第一行选择了完全不同的值“John”。 如果发生这种联系(两种情况),则移至前一个向量 V (n-1);在上述情况下,它应该移至 v4。 v4 中获得多数票的值是“Isaac”。 我找到了一个解决方案来打破第一个平局的情况,但对于上一个例子中提到的情况我却找不到。 我已经为此工作了好几天了,我真的需要它。预先非常感谢您。

I want to break ties for a majority/Plurality voting using the below logic :

  1. I have two vectors vName and v2. v1 and v2 both character vectors and have 5 rows.
  2. A tie happens if :
    a- if every row in vName has a different corresponding value in v2
    b- if two rows in vName have the same corresponding value in v2 and another two rows have the same value (different from the value picked by the two previous rows) and the last row has a different corresponding value in v2 that is different of the two different values picked by the previous rows (the two rows and the other two rows)

For example :
say,we have a vector :

vName<-c("x1","x2","x3","x4","x5") 

v5<-c("John","Abraham","Isaac","Abraham","Isaac")
v4<-c("John","Abraham","Isaac","Isaac","Isaac")

the 2nd and 4th rows picked the value "Abraham" and the 3rd and 5th picked the value "Isaac" whereas the 1st row picked a completely different value "John".
if such ties happen (both scenarios) then move to the previous vector V (n-1); in the above case it should move to v4. the value with the majority vote in v4 is "Isaac".
I found a solution to break the tie for the first tie situation, but for the one mentioned in the previous example I couldn't.
I was really working on it for days now and I really need it. Thank you so much in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2025-01-18 12:12:16

您可以在数据框中使用分组和切片。

library(dplyr)
v1 <- c("x1","x2","x3","x4","x5") 
v2 <- c("John","Abraham","Isaac","Abraham","Isaac")
    
df <- tibble(v1,v2)
# group by the v2 column, select the head of each group and then sort by the v1 column
ties <- 
    df %>% 
    group_by(v2) %>% 
    slice_head(n=1) %>% 
    ungroup() %>% 
    arrange(v1)

ties
# A tibble: 3 × 2
  v1    v2     
  <chr> <chr>  
1 x1    John   
2 x2    Abraham
3 x3    Isaac 

如果您想选择随机条目而不是第一个条目,则可以将 slice_head 更改为 slice_sample

You could use grouping and slicing in a data frame.

library(dplyr)
v1 <- c("x1","x2","x3","x4","x5") 
v2 <- c("John","Abraham","Isaac","Abraham","Isaac")
    
df <- tibble(v1,v2)
# group by the v2 column, select the head of each group and then sort by the v1 column
ties <- 
    df %>% 
    group_by(v2) %>% 
    slice_head(n=1) %>% 
    ungroup() %>% 
    arrange(v1)

ties
# A tibble: 3 × 2
  v1    v2     
  <chr> <chr>  
1 x1    John   
2 x2    Abraham
3 x3    Isaac 

You can change the slice_head to slice_sample if you want to choose a random entry rather than the first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文