在 R 中使用组合打破平局
我想使用以下逻辑打破多数/复数投票的平局:
- 我有两个向量 vName 和 v2。 v1 和 v2 都是字符向量,有 5 行。
- 如果出现以下情况,则出现平局: 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 :
- I have two vectors vName and v2. v1 and v2 both character vectors and have 5 rows.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在数据框中使用分组和切片。
如果您想选择随机条目而不是第一个条目,则可以将
slice_head
更改为slice_sample
。You could use grouping and slicing in a data frame.
You can change the
slice_head
toslice_sample
if you want to choose a random entry rather than the first.