计算向量中的对数 [英] Counting the number of pairs in a vector

查看:206
本文介绍了计算向量中的对数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Suppose that I have the following vector:

我想知道不同对的数量以下类别:

V<-c(-1,-1,1,1,1,-1,-1,1)

(1,1),(-1,1),(1,-1)和(-1,-1)

And I want to know the number of different pairs in the following categories:

在我的例子中,每个都有一对。

(1,1), (-1,1), (1,-1), and (-1,-1)

我一直试图解决这个问题的功能 split setkey ,但我不能做分类。

In my example, there is exactly 1 pair of each.

推荐答案






这里是一个更好的选择,也使用 split ,按照@ Martin Morgan的回答模式:


Here is a better alternative that also uses split, following the pattern of @MartinMorgan's answer:

table(split(V,1:2))
#     2
# 1    -1 1
#   -1  1 1
#   1   1 1

这篇关于计算向量中的对数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆